They interviewed before me testers with "Java and Selenium" experience and skills in the resume that in reality did not know much of either Java or Selenium.

So the company gives me a Java test.

Such as

BINARY GAP

A binary gap within a positive integer N is any maximal sequence of consecutive zeroes that is surrounded by ones at both ends in the binary representation of N.

Examples:

Number 9 has the binary representation 1001 and contains a binary gap of length 2.

The number 529 has binary representation 1000010001 and contains 2 binary gaps: one of length 4 and one of length 3.

I get a pen and paper and 1 hour to do it.

Pretty stressful, isn't it?

After 1 hour, I came up with this solution.

Not too pretty but somewhat functional:

import static org.junit.Assert.*; import java.util.ArrayList; import java.util.List; import org.junit.Ignore; import org.junit.Test; public class BinaryGapTestsInitial { @Test public void testLongestGap() { int number = 789; //convert from int to binary; the list is in reversed order ListreversedBinaryList = new ArrayList (); int tempNumber = number; while (tempNumber != 0) { reversedBinaryList.add(tempNumber % 2); tempNumber = tempNumber / 2; } //reverse the list List binaryList = new ArrayList (); for (int i = reversedBinaryList.size() - 1; i >= 0; i--) binaryList.add(reversedBinaryList.get(i)); //verify that the binary list is correct int numberFromBinary = 0; for (int i = 0; i < binaryList.size(); i++) if (binaryList.get(i) == 1) numberFromBinary += Math.pow(2, binaryList.size() - i - 1); System.out.println(binaryList); System.out.println(numberFromBinary); int longestGap = 0; for (int i = 0; i< binaryList.size(); i++) if (binaryList.get(i) == 0) { int j; int gap = 1; for (j = i + 1; j < binaryList.size(); j++) if (binaryList.get(j) == 1) { gap += j - i - 1; i = j - 1; break; } System.out.println(gap); if (gap > longestGap) longestGap = gap; } System.out.println("longest gap = " + longestGap); } }

This code is probably a good candidate for the right side of the next image:

**Is this the best and most amuzing measure of bad code?**

Now, assume that the company really wants to know how well you know Java.

So they want not only working code but good code.

Something that looks like this:

import static org.junit.Assert.*; import java.util.ArrayList; import java.util.List; import org.junit.Ignore; import org.junit.Test; public class BinaryGapTests { private ListtoBinary(int number) { List list = new ArrayList (); while (!is0(number)) { list.add(0, number % 2); number = number / 2; } return list; } private int toInt(List list) { int number = 0; for (int i = 0, listSize = list.size(); i < listSize; i++) if (is1(list.get(i))) number += Math.pow(2, listSize - i - 1); return number; } private int last0Index(List list, int index) { int lastIndex = 0; for (int i = index; i < list.size(); i++) if (is1(list.get(i))) { lastIndex = i - 1; break; } return lastIndex > 0 ? lastIndex : list.size() - 1; } private int first0Index(List list, int index) { int firstIndex = -1; for (int i = index; i < list.size(); i++) if (is0(list.get(i))) { firstIndex = i; break; } return firstIndex; } private List shrinkList(List list, int index) { return list.subList(index, list.size()); } private Boolean is0(int number) { return number == 0; } private Boolean is1(int number) { return number == 1; } private int gapSize(int startIndex, int endIndex) { return endIndex + 1 - startIndex; } private Boolean includes0(List list) { return list.lastIndexOf(new Integer(0)) > 0; } private int longestGap(List binaryValue) { int longest0Gap = 0; List list = binaryValue; if (includes0(list)) while (!list.isEmpty()) { int first0Idx = first0Index(list, 0); int last0Idx = last0Index(list, first0Idx); longest0Gap = (gapSize(first0Idx, last0Idx) > longest0Gap ? gapSize(first0Idx, last0Idx) : longest0Gap); list = shrinkList(list, last0Idx + 1); } return longest0Gap; } @Test public void testLongestGap() { List binaryValue = toBinary(808); System.out.println(binaryValue); System.out.println(longestGap(binaryValue)); assertTrue(longestGap(binaryValue) > 0); } @Ignore public void testBinaryValue() { int number = 7; assertEquals(number, toInt(toBinary(number))); } }

Can you write code like this in an hour?

It is probably doable with practice but definitely not easy.

So,

**if you dont know Java well, think again next time you add Java to your resume.**

Because a company may ask you to do a test like the one from above.

## 0 Comment to "Do you know JAVA? Prove it"

## Post a Comment