Intent of my Blog
This blog is about one of the question that i was asked in an interview two years back. I forgot this question but yesterday i again faced this question and this is a “java puzzler”.
Question
Yesterday while looking for videos on “java puzzlers” i found out link on youtube by Joshua Bloch.When i started viewing this video one of the puzzle reminded of a question i was asked in an interview two years back. Although this question is not in Java(TM) Puzzlers: Traps, Pitfalls, and Corner Cases but it is one of the question i thought worth sharing.Try this question and have fun.
public class JavaPuzzler{ public static void main(String[] args) { HashSet<Short> s = new HashSet<Short>();//1 for(short i = 0; i<100;i++){//2 s.add(i);//3 s.remove(i-1);//4 } System.out.println(s.size());//5 } }
Before viewing the answer of this question please try guessing the answer to this question and then run code in eclipse to check whether you what you were thinking matches the answer.If the answer amaze you it is a java puzzler.
Answer
Answer of this puzzler is 100.
To understand why we get 100 as answer lets try to understand the code line by line.
In line 1 we created an HashSet which is of type java.lang.Short .
In line 2 we are doing a for loop
In line3 we are adding short primitive which will be autoboxed to Short object to the HashSet Collection.
In line 4, we are trying to remove an element from HashSet, which we added just before the current element. But there is a small gotcha (can you guess what is it?), the gotcha is when we do s.remove(i-1), first of all expression (i -1) is evaluated in which short is widened to int and then int is converted to Integer object. This is due to autoboxing in java 1.5 version.If you look into the javadoc of remove method of HashSet, you will find that it takes an Object, so you can remove Integer objects from HashSet of Short objects, but this will not work as a result none of the Short objects will get removed.
So in line 5 we get output as 100.
Hope you find this puzzle interesting, i will strongly recommend viewing this presentation.
You can share any of yours interview question which you think is a “java puzzler”.
Ugh. Seriously, if someone asked me THAT in an interview, I’d just get up and leave. Nothing productive can be created in an environment where these kind of details matter, nor would I like to be hounded to death by technical issues.
A much better question is “this happens – can you explain how’d you detect it (testing), and prevent it from happening again (informing other people in your team via email / stand-up meetings / code reviews)”. Maybe even use it as a guideline “explain autoboxing, explain hashset, explain java 5 loops”. But never “why this happens” or “what output would be printed”.
Thanks for your comment. You have made a very Good point.
Fantastic puzzle. Thanks for your beautiful explanation.
A good gotcha. I wrote about similar boxing gotchas -> http://www.certpal.com/blogs/2009/08/autoboxing-unboxing-gotchas/.
This is a very unfair interview question however. My thoughts mirror Ran Biron’s in this regard.
Yes i also think it is an unfair interview question
It is a nice puzzle, but I found the lack of interpunctuation and formatting even more puzzling… I had a hard time reading the sentences about line 4.
Thanks for your comment. I will try to keep punctuation and formatting right next time.