Tuesday, February 17, 2009

Crazy Java Puzzler

Here is a puzzle for you. As the title promises, yes, it's quite crazy. It is assumed that you already are a die-hard Java programmer that knows the language better than the skin of his very palm. Ok, maybe not that much, but anyway.

As a little history, I had posted this on the Hellenic (i.e. Greek) Java User Group, which used to have an online forum, now defunct (too bad). So if you happen to be on those few who happened to see the solution there, just restrain yourself, I don't want any stinkin' spoilers!

Enough of intro. Here is the code:

interface id {
  <id> Void read(List<id> list);
  <id> Void read(List<id> list);
  <id> Void read(List<id>... list); //*
}

The puzzle: What is the shortest edit (measured in inserted/deleted characters) that makes this program compilable? (While keeping the read methods overloaded).

(We're not talking any kind of cheat like compiling with some arbitrary compiler, just the plain Java compiler. Oh, and commenting out lines of code is the wrong answer too).

The problem, obviously, is that the two methods produce a name clash.Well, it's all yours! Hit it hard! Can you nail it?

* [10-Mar-10] As Sureth below points, a solution would be appending "[]" to a list, a 2-character ending. Indeed. Sloppy of me. That basically ruins the fun, so I have to this third overloading to avoid [] and varargs solutions. Now (hopefully) only the intended solution is there, hopefully in not too artificial a puzzle. Oh, by the way, trying this problem is not a waste of time: it reveals a deeper knowledge about the java platform and gives a new (for those unaware of it) technique that can be used in API design. It's already the "magic sauce" of the nice API of a popular testing-related framework.

9 comments:

  1. 1. Τα import μετράνε, ε?
    2. Το Void είναι το γνωστό void ή είναι Void object?


    btw, σε πρόσθεσα στα link μου. Δε νομίζω να σε πειράζει :-)

    ReplyDelete
  2. Void != void. Kotso, English please re! :)

    ReplyDelete
  3. Dimitris - Sam here, from www.java-twisters.blogspot.com Sorry to use this post as a conversation ground.

    1. Yep something wrong with blogtrog - the service I used to display formatted code - So the puzzle is not being seen - done a temporary fix for now. Thanks for pointing out.

    2. It *might* be an time worn puzzle if you had used a null reference to call the static - No its not - its something much simpler :)Feel free to have a look again ...

    Currently, the questions in twister are supposed to be beginer level - re-thinking if I should go thougher - maybe in some time I might.

    3. Nice puzzle - I could not think of any solution except converting the 'List' to 'ArrayList' in the seacond read - so 5 additional char. Somehow I get the feeling I am missing something here :)

    4. Just a thought - why two languages? Makes it hard to navigate your blog :(

    ReplyDelete
  4. @Saifuddin: sorry about the late response, I had misconfigured the blog so I didn't get email updates on comments :).

    I guarantee you that the puzzle is both very unique and deep. Its roots lie at the dark corners of the VM :)

    A simple answer to break the name clash is just add "..." to a method parameter list, i.e. varargs. This is 3 chars, and a quite content-free solution (that would hardly make even a beginner's puzzle).

    Sorry about the the intermingled Greek, some friend from there had popped up :)

    ReplyDelete
  5. Shortest edit
    void read(List [] list);

    Suresh

    ReplyDelete
  6. sigh. Indeed that's "the solution", as the problem is given. In the original problem I also included the

    " Void read(List<id>... list);" signature, to avoid exactly this (does [] feel like a puzzle solution?)

    ReplyDelete
  7. I guess a year and a half is enough. The solution is...(tam tam tam tam taaaam)...

    Change List<id> to List<Void>

    I have found a fabulous explanation for this but this textbox is too small for it to fit. :)

    ReplyDelete