Interview replay

Full round replay — leaks

10 minsenior315 yrs

Timed verbal replay with pass/fail criteria per follow-up.

How to run this

Answer out loud, timed. Do not read the entry first. Then compare against "The Answer" and "Interviewer's Next Move" and mark yourself.

The opener

How do you get a memory leak in a garbage-collected language?

Budget: 45 seconds. Going long here is itself a fail signal.

Follow-ups

  1. 1. “Name the four you would check first.

    Testing: Is this a list they have used, or one they have read?

    Scoring

    Pass: Static collections that only grow; listeners never unregistered; ThreadLocals on pooled threads; ClassLoader leaks in anything that redeploys. Gives a different fix for each.

    Fail: 'Not closing resources' — a real bug, but not this one.

  2. 2. “Why is a ThreadLocal a leak on a pool but not on a plain thread?

    Testing: Do they know the entries are cleaned up on thread death?

    Scoring

    Pass: Entries go when the thread dies, and pooled threads are built not to. Notes that ThreadLocalMap keys are weak but VALUES are strong, so the weak key does not save you. remove() in a finally.

    Fail: 'ThreadLocal is always a leak', or thinks the weak keys handle it.

  3. 3. “You have a heap dump. What do you look at first?

    Testing: Retained vs shallow — the single most useful habit here.

    Scoring

    Pass: Retained size via the dominator tree, then the shortest path to a GC root from the biggest dominator. That path is the bug.

    Fail: 'Sort by instance count' or reaches for an allocation profiler.

  4. 4. “Is a cache a leak?

    Testing: Judgement.

    Scoring

    Pass: Without an eviction policy, yes — a leak with a hit rate. The question to ask of any cache is what removes an entry: a size bound, a time bound, or explicit invalidation.

    Fail: 'No, caches are supposed to hold things.'

  5. 5. “Someone proposes WeakHashMap for the session cache. React.

    Testing: Do they know the specific failure, not the general caution?

    Scoring

    Pass: Weak keys only help if nothing else strongly references the key — and the value usually does. Eviction also becomes unpredictable. A size bound plus TTL is boring and correct.

    Fail: 'Good idea, weak references prevent leaks.'

  6. 6. “Heap after full GC is flat and high, not rising. Leak?

    Testing: Can they rule a leak OUT? Most people can only rule one in.

    Scoring

    Pass: No — that is a workload that needs that much live data. A leak shows a RISING floor across runs of the same input. Distinguishing them before opening a dump is the point.

    Fail: 'Yes, the heap is nearly full' — treating high usage as evidence.

Score yourself

6/6 — you can hold this topic at senior level 4-5 — solid; reread "Interviewer's Next Move" 0-3 — reread "Understand It" and redo the Challenge tier

← Back to How do you get a memory leak in a garbage-collected language?