Category Archives: Java

Fast, thread-safe Thunk in Java

6 months ago or so I was going through Wealthfront’s website when I stumbled upon their recruiting puzzle for the “Backend software engineer” position. The page is still online but not linked under their Available Positions list so I guess it’s alright to publicly discuss that puzzle.

The puzzle reads

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

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

Read the rest of this entry