Interview replay

Full round replay — immutability

10 minintermediate110 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 make a class genuinely immutable?

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

Follow-ups

  1. 1. “Is a class with all final fields immutable?

    Testing: The whole topic in one question.

    Scoring

    Pass: No — final freezes the reference, not the object. A final List field is fully mutable. You also need no mutators, a copy in the constructor, a copy in any getter returning a mutable type, and the class final.

    Fail: 'Yes' — or lists the recipe without being able to say what final actually does.

  2. 2. “Why copy in the constructor as well as the getter?

    Testing: Do they see them as two different holes?

    Scoring

    Pass: Different leaks. Without the constructor copy, the caller who supplied the collection keeps a live reference. Without the getter copy, every reader gets one. Fixing only the getter is the common half-done version.

    Fail: Treats them as the same thing, or only knows the getter half.

  3. 3. “Is Collections.unmodifiableList enough?

    Testing: View vs copy — the mistake that looks like the fix.

    Scoring

    Pass: Not for a field. It is a read-only VIEW: it blocks writes through the wrapper and reflects every change to the backing list. Fine as a return value; List.copyOf is what actually severs it.

    Fail: 'Yes, it makes it immutable.'

  4. 4. “What does immutability buy you at runtime?

    Testing: Beyond tidiness.

    Scoring

    Pass: Thread safety with no synchronisation, safe publication via the final-field guarantee, and a hash that cannot change so it is safe as a map key.

    Fail: 'It's cleaner' or 'good practice'.

  5. 5. “How would you handle a byte[] field?

    Testing: The case with no clean answer.

    Scoring

    Pass: clone() on both sides — there is no immutable array. And notes that array equals/hashCode are identity-based, so if it is part of equality you need Arrays.equals and Arrays.hashCode explicitly.

    Fail: 'Same as a List' — there is no Arrays.copyOf that makes it immutable.

  6. 6. “Someone says defensive copying is too slow. React.

    Testing: Judgement plus a specific fact.

    Scoring

    Pass: Measure it, and expect there to be no copy: List.copyOf returns the input unchanged when it is already immutable. If a copy does happen it is a shallow array copy. Push the immutability to the caller rather than removing the defence.

    Fail: Either caves immediately or refuses on principle with no measurement.

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 make a class genuinely immutable?