java

The tech interview “test”

Have you ever gone on a job interview and had a written technical “test” such as the following?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1.) What is the result of compiling and running the following?
    Select the one correct answer.

public class badTest {
  public static void main(String args[]) {
    int i, j;
    int k = 0;
    j = 2;
    for (i = 0; i <= 9; ++i) {
      j = i + 2;
    }
    k = j = i;
    System.out.println(k);   
 }
}

A. The program does not compile as k is being read before being initialized.
B. The program does not compile because of the statement k = j = i;
C. The program compiles and runs printing 0.
D. The program compiles and runs printing 1.
E. The program compiles and runs printing 10.
F. The program compiles and runs printing 11.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2.)  What happens when the following program is compiled and run?

public class lameTest {
  int i = 0;
  public static void main(String args[]) {
     int i = 1; change_i(i); System.out.println(i);
  }
  public static void change_i(int i) {
     i = 2; i *= 2; new lameTest().change_I_TOO(i);
  }
  protected void change_I_TOO(long i) {
    short j = 100; do {
      i = j / i + 3; j = (short) i;
    } while (j > 22);
  }
}

A. The program does not compile.
B. The program prints 0.
C. The program prints 1.
D. The program prints 2.
E. An exception is thrown.
1
2
3
4
5
6
7
8
9
10
11
12
3.) In the following code, select all the lines that will not compile.

public final abstract class terribleTest {
  public void main(final String args[]) {
    char p;
    int i;
    p = "B";   
    i = p;   
    p = i + 1; 
    p++;
  }
}

If so, did you you get an offer? Did you take it? Perhaps you’re a hiring manager and you’ve given a test like this before. Maybe you still do. If so, why? It’s been my experience that anytime someone asks you to take a written exam such as this, you should turn around and run away, fast! There are so many problems with a test such as this, it’s hard to know where to begin. First, look at the quality of the code. Do you write code like that? Do you want to? What if you got in the shop and all the code was similar to this? Second, who writes code without an IDE? Who cares if the code, as written on paper, compiles or not? Developers have a hard enough time keeping variables, tasks, and trains of thought clear in their head. They don’t need the extra burden of trying to keep track of every little syntax error. That’s why we have tools like modern IDEs and code analysis. Third, does this even count as programming? What problem does it solve? What is the test giver trying to learn? Does it help them determine who is going to be a good candidate and who isn’t? Not in my judgment.

I’ve had the unfortunate experience of taking a test like this on two difference occasions. The first time, I suffered through and did the best I could. I wasn’t offered a job and now know I was better off for it. The second time, I didn’t take the test. Instead, I spoke to the interviewer about my reservations, why he felt the need to use this type of test, and what he was hoping to gain. It turns out, he wasn’t a Java developer and he’d had the outgoing Java dev create the test in order to judge if a person knew software development or not. Whatever the case, this is decidedly not software development. At least not any that I’d want to be a part of.

So, if you’re ever asked to take a test such as this, thank the interviewer for the huge favor they have done you. They told you exactly what it’s like to work for them. It would likely involve scary code, lack of proper development tools, and the pain of cleaning up someone’s mess. If you’re thinking about giving a test like this, please think again. Consider better ways you might use to determine if someone has the qualities you’re looking for. Talk about some of the problems you’re having. Show them some of your code. Have them bring in some of their code. Discuss what makes it good or how it could be improved. Whatever you do, don’t try to figure out if they are a good fit based on something that is so far removed from what a development job is really all about. Trust me. Future interviewees will thank you and you’ll be rewarded with better hires.

Google collections 1.0 released

Google Collections just released version 1.0 final, so now everyone can upgrade and be confident the interfaces aren’t going to change.  I’ve been using the project for awhile now.  It’s really popular, but I’ve been in a couple of shops that haven’t been using it for whatever reason.  I wrote a couple of very simple examples to just showcase some cool features.  Java 1.5 is required.

Simple creation:

1
2
3
List<Integer> emptyList = Lists.newArrayList();
Set<Integer> emptySet = Sets.newHashSet();
Map<Integer, String> emptyMap = Maps.newHashMap();

Immutables:

1
2
3
4
5
6
7
8
9
10
11
List<String> list = ImmutableList.of("One", "Two", "Three", "Four");
Set<String> set = ImmutableSet.of("One", "One", "Two", "Three");
Map<Integer, String> map = ImmutableMap.of(1, "One", 2, "Two", 3, "Three");

//list.add("fail");   // throws java.lang.UnsupportedOperationException
//set.add("fail");    // throws java.lang.UnsupportedOperationException
//map.put(1, "fail"); // throws java.lang.UnsupportedOperationException

System.out.println(list);
System.out.println(set);
System.out.println(map);

prints:

1
2
3
[One, Two, Three, Four]
[One, Two, Three]
{1=One, 2=Two, 3=Three}

Multis:

1
2
3
4
5
6
7
8
9
10
11
Multiset<String> multiset = HashMultiset.create(Arrays.asList("One", "One", "One",
"Two", "Two", "Three"));
Multimap<String, String> multimap = HashMultimap.create();
multimap.put("1", "One");
multimap.put("1", "ONE");
multimap.put("1", "oNe");
multimap.put("2", "two");
multimap.put("2", "t-w-o");

System.out.println(multiset);
System.out.println(multimap);

prints:

1
2
[Three, Two x 2, One x 3]
{2=[two, t-w-o], 1=[oNe, ONE, One]}

Bidirectional map. Changes to one side affect the other.

1
2
3
4
5
6
7
BiMap<String, String> bimap = HashBiMap.create();
bimap.put("ORCL", "Oracle");
bimap.put("RHT", "Red Hat");
bimap.put("VMW", "VMware");

System.out.println(bimap);
System.out.println(bimap.inverse());

prints:

1
2
{VMW=VMware, RHT=Red Hat, ORCL=Oracle}
{Red Hat=RHT, VMware=VMW, Oracle=ORCL}

MapJoiner

1
2
3
MapJoiner mapJoiner = Joiner.on(", ").withKeyValueSeparator(
"'s stock symbol is ");
System.out.println(mapJoiner.join(bimap.inverse()));

prints:

1
Red Hat's stock symbol is RHT, VMware's stock symbol is VMW, Oracle's stock symbol is ORCL

ConcurrentMap with values that expire.

1
2
3
4
5
6
7
8
ConcurrentMap<Integer, String> concurrentmap = new MapMaker().expiration(2L,
TimeUnit.SECONDS).makeMap();
concurrentmap.put(1, "One");
concurrentmap.put(2, "Two");

System.out.println("Map size = " + concurrentmap.size());
Thread.sleep(3000);
System.out.println("Map size = " + concurrentmap.size());

prints:

1
2
Map size = 2
Map size = 0

Map difference compares two maps

1
2
3
4
5
6
7
8
9
10
11
Map<Integer, String> mapOne = ImmutableMap.of(100, "1 hundy", 200, "2 hundy", 300,
"3 hundy", 1000, "10 hundy");
Map<Integer, String> mapTwo = ImmutableMap.of(200, "2 hundy", 300, "3 hundy", 400,
"4 hundy", 1000, "1 dime");

MapDifference<Integer, String> diff = Maps.difference(mapOne, mapTwo);

System.out.println("Same key, different element: " + diff.entriesDiffering());
System.out.println("Same key, same element: " + diff.entriesInCommon());
System.out.println("Elements only on left: " + diff.entriesOnlyOnLeft());
System.out.println("Elements only on right: " + diff.entriesOnlyOnRight());

prints:

1
2
3
4
Same key, different element: {1000=(10 hundy, 1 dime)}
Same key, same element: {200=2 hundy, 300=3 hundy}
Elements only on left: {100=1 hundy}
Elements only on right: {400=4 hundy}

a
a

There is a lot more in the package than just these, but this is a good start. I’ll put up a post on some of the other features that you might find useful later.