seralize my thoughts into posts

As I posted before I’m  Looking for an outstanding Software Engineer. Interested ? try solve this puzzle, and contact me via twitter or drop your CV via mail – yonatan [at] outbrain [dot] com.

puzzle

Provide an alternate implementation of the following class,  Thunk, that uses no conditional logic. For extra credit, make it thread-safe.

abstract class Thunk {
  private boolean evaluated;
  private T value;
  public T get() {
    if (!evaluated) {
      value = compute();
      evaluated = true;
    }
    return value;
  }
  abstract protected T compute();
}

If you thought about something like this, try to find a more elegant solution.

abstract class Thunk<T> {
  private T value;
  public T get() {
    try {
      value.toString();
    } catch (NullPointerException e) {
      value = compute();
    }
    return value;
  }
  abstract protected T compute();
}

Comments on: "Can you Solve the Puzzle ?" (4)

  1. Miki Tebeka said:

    Something like [link was removed] ?

  2. nice solution.
    Removed the spoiler link 🙂

  3. I must be honest I found the solution on google …

  4. I guess you right, but it takes all the fun out 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: