Imagine a bacon-wrapped Ferrari. Still not better than our free technical reports.

Solutions to the [difficult] “Winter is Coming” Java Puzzle

Over the recent winter holidays, we made a Java brain-teaser that was quite different from our regular puzzles. We definitely succeeded in making it different–so different in fact, that it wasn’t as popular as we had expected!

As there have been many requests for solutions, we are writing this followup post to explain the details of this puzzle. We asked you to guess a function and then to implement it as a Java method. The correct solution would satisfy our provided unit tests. So it’s kind of like test-driven development, but you only have the tests and need to guess the function.

Let’s go through a reference solution and look how we thought you would entertain yourself with this puzzle. First of all we need tools required by the setup: hg, Java 7, Gradle. Here’s why you need to clone a repo:

hg clone
cd xmas2012
gradle test

This will result in all tests failing, so good, we are ready for coding! By carefully examining the problem, you can see that we need to convert a pair of integers into a string, and that the possible values of those strings are related to Java (who would have thought!)

But there’s no other information that can help us, so we’re stuck. As usual “when you hit a dead end, go back to the beginning to find your way”.

Check the source, Luke

So let’s trace back to the beginning. The beginning is the blog post itself! Lets check the source code of that page.


We called this TIP 2 for no good reason (there is no TIP 1). The link points to a text version of a JVM specification, so an educated guess tells us that the words are taken from this file. Now we just need to figure out a relation between the integers and words in that book.

Let’s take a single test and try to disassemble it.

 public void test7() {
   assertEquals("Bytecode", MerryXmas.declarationLookup(7, 2));

 public void test6() {
   assertEquals("malicious", MerryXmas.declarationLookup(19, 10));

Hmm, there are many bytecode words in the txt file, so it doesn’t help us immensely. So maybe a less common word would push us unto the right path? (or get distracted and learn more about Java Bytecode)

How about malicious? Indeed, there is only one occurrence of malicious in the text file. It sits on what looks like page 20 of the specification book. Now let’s check one more thing; yep, page numbers start from 1, not like an intelligent person would make them starting from 0. So I suspect, that the first number in the MerryXmas.declarationLookup(19, 10)); is a page number. A quick test verifies that bytecode is mentioned on page 8, so this hypothesis holds.

Now, if the first index is a page number, the second one could naturally be a line number! Otherwise nothing makes sense, right? Gotcha, eighth page, third line has the word bytecode, and 11th line on page 20 features that lonely word malicious.

Now we’re almost there, I can feel a tingling in my fingers! We just need to find out which word we pick from the line. Let’s check more examples.

7, 2   => 1 (first word of the third line on page 8)
4, 7   => 1
12, 3 => 4
2, 12 => 6

A pattern emerges. Let’s check on larger numbers.

 public void test5() {
   assertEquals("virtual", MerryXmas.declarationLookup(979, 785));

Page 979 does not exist, and I think the overflowing is a known concept, so we just take 979 % 600 (number of pages) = 379

785 % 35 (number of lines on page 380, don’t forget indexing is from 0) = 15

GCD (979, 785) = 1, so we need the second word. And it is “virtual” as the test expects.

Now we just need to carefully write it down and see if we got it right or some tests still fail. Our implementation is available on github.


This puzzle was way different from our previous set of puzzles and we probably made too many assumptions in the process. We’ll definitely try this quest style of approach again but we’ll make some adjustments on the way. For example, we can make the steps shorter, give feedback when a step has been successfully finished, and respond with some tips along the way! We did succeed in couple of things, like popularizing some of the tools that we enjoy a lot, like BitBucket, Gradle and Unit Testing.

We are more than happy to hear your comments on how you enjoyed this puzzle and what we can do better next time. Comment away!