Java Puzzle

As big fans of the book “Java Puzzlers” by Joshua Bloch and Neal Gafter, we’re always looking for new Java puzzles to solve. Since we haven’t seen good ones in a while, and as a tribute to their Java brainteasers we’ve decided to add a puzzle of our own. In their own words: “By working through the puzzles, you will become less likely to fall prey to these dangers in your code and more likely to spot them in code that you are reviewing or revising”. And besides, it’s also really fun. To spice it up a bit more we’ve added a $250 Amazon gift card to the one developer who manages to solve it. In one move. And give the right explanation as to what exactly happened there. We hope your kung fu is strong today.

The basics

Two teams are playing basketball, each is scoring points against the other and then suddenly – boom and game over. The project structure is pretty simple:

+-- simulator
|   +--
|   +--
+-- data
|   +--
|   +--

Two game simulator threads invoked by the main class, a simple data structure to hold the scores and the rest is up to you.


1. Head over to:
2. Clone, compile, run and see it crash and burn.
3. Solve the bug by changing ONE single keyword/token/identifier: it could be by replacing it, adding one, or deleting one.
4. Explain what caused the bug and how your solution fixes it.
5. Send us the solution and explanation to by Friday September 12, 12pm (PDT)

Update (12/9): We’d like to thanks everyone who participated and attempted to solve the puzzle, hope you had a good time! You can find the correct solution here – follow us on twitter to get updates on new puzzles.

** Please note that the explanation will be a crucial part of the answer. A wrong or an incomplete explanation may disqualify you even if you fixed the bug.

In case there’s more than one developer who solves the puzzle correctly, we’ll randomly choose one winner. In any case, the right answer will be published on the following week and all those who got the right answer will receive their recognition.

If you have any questions, please post them in the comments section below. Oh, and if you reach an answer, please don’t spoil it for everyone. At least not before the deadline. Good luck! 🙂

Java Developers – Know when and why production code breaks – read more


Already using Java 8? Try OverOps for Java 8

Niv is co-founder and VP R&D at OverOps. In his free time Niv is an avid basketball fan and a student of languages.
  • Lukas Eder

    Piece of cake. Thanks for the nice puzzle! 🙂

    • Niv Steingarten

      Thanks Lukas, happy to hear you liked it!

  • BeepDog

    Fun puzzle!

    • Niv Steingarten

      Thanks BeepDog!
      Glad you enjoyed it.

  • traptrap

    Maybe just a bug of the HotSpot JVM?

  • Sergio Tomas

    Mmm, the program runs and exits without problems for me out of the box, printing the score for both teams. No errors, crashes, or anything at all :S

    • ray

      same here

      • Niv Steingarten

        Hi Ray,
        See my reply to Sergio above.
        Let me know if it helps.

        • ray

          I tried both from command line and from eclipse, but everything looks good. I am using oracle JDK1.7

          • Niv Steingarten

            I’m assuming you’re running a multi-core machine, right?
            Do you have Java 6/8 lying around by any chance? Might be worth a try.

          • Guest

            seems JDK8 works fine by me…

          • ray

            seems JDK8 works fine by me, no crash…

    • Niv Steingarten

      Hi Sergio,
      How are you running the program?
      This bug is non-deterministic in nature. Try running the program from a different environment (e.g. run it using Eclipse/IntelliJ if running it via the command line doesn’t work).
      Make sure you use Oracle/OpenJDK Java 6, 7 or 8.

      • Sergio Tomas


        I’m running Oracle’s JDK 7, but it was running it from command line just executing. I run it a lot of times, so I think I will have to try another way then. I expected it to be somehow random but I didn’t expect for the place you run it from to affect so much :S

        • Brian Stempin

          Using Oracle’s JDK 8, the bug happens almost every time. I found that it almost never happened when running it in debug mode.

  • Iñaki Perez de Albeniz

    I enjoyed dealing with this puzzle. Thank you!

    • Niv Steingarten

      Thanks Iñaki, I’m very happy to hear that!

  • Srini Palla

    Submitted my response. Good one.

  • Vlad Mihalcea

    Nice puzzle. Let’s see if my fix is worth 250$. Anyway I am looking forward to seeing the winning explanation.