Interview replay

Full round replay — volatile and synchronized

10 minintermediate112 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

What does volatile guarantee, and what does it not?

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

Follow-ups

  1. 1. “Why is a volatile counter still broken?

    Testing: Can they decompose count++ without being led there?

    Scoring

    Pass: count++ is read, add, write. volatile makes each step visible without making the trio indivisible, so two threads read the same value and both write back the same increment. At scale most increments are lost.

    Fail: Says it is only broken in rare interleavings, or that volatile makes it atomic.

  2. 2. “Name the three problems concurrency has to solve.

    Testing: The framing the whole topic rests on.

    Scoring

    Pass: Atomicity, visibility and ordering. volatile addresses visibility and ordering; only mutual exclusion or a CAS type gives atomicity.

    Fail: Treats thread-safety as one undifferentiated problem.

  3. 3. “Does a synchronized instance method exclude a synchronized static method in the same class?

    Testing: The question that separates mechanism from keyword.

    Scoring

    Pass: No. The instance method locks `this`, the static method locks the Class object. Different monitors, no exclusion.

    Fail: Says yes because both are synchronized and in the same class.

  4. 4. “I made my HashMap field volatile. Is the map thread-safe?

    Testing: Reference versus contents.

    Scoring

    Pass: No — volatile protects the reference. Concurrent puts corrupt the map exactly as before. Either use ConcurrentHashMap, or keep volatile and replace the whole map with a new immutable copy on each write, which is right when reads dominate.

    Fail: Accepts it, or says volatile makes the object thread-safe.

  5. 5. “Why does double-checked locking need volatile?

    Testing: Ordering as a distinct problem.

    Scoring

    Pass: Without it the write publishing the reference can be reordered ahead of the writes initialising the object, so another thread sees a non-null reference to a half-constructed object. That is ordering, not visibility.

    Fail: Says it is about visibility, or that volatile is optional there.

  6. 6. “Your 50-thread test passes. Is the code thread-safe?

    Testing: The senior-level answer.

    Scoring

    Pass: No conclusion follows. A visibility bug depends on the JIT, core count and the CPU's memory model — it can hide for years and surface on a different architecture. Correctness is argued with happens-before, not demonstrated by a green run.

    Fail: Treats the passing test as evidence of safety.

  7. 7. “When is volatile the right tool?

    Testing: Can they name the cases, not just the anti-cases?

    Scoring

    Pass: A one-way flag one thread writes and others read; safe publication of an immutable object; and double-checked locking. Nothing involving a read-modify-write.

    Fail: Cannot name a legitimate use, or names a counter.

  8. 8. “AtomicInteger or a lock under heavy contention?

    Testing: Whether they know CAS has a cost.

    Scoring

    Pass: CAS retries when the swap fails, so at high contention an atomic can burn CPU losing races while a lock parks the thread and lets one winner through. Default to the atomic, measure before deciding.

    Fail: Says atomics are always faster.

  9. 9. “Two atomic fields that must stay consistent with each other. What do you use?

    Testing: The limit of atomics.

    Scoring

    Pass: Neither field being atomic helps — an invariant over two fields needs one critical section over both, or a single immutable object holding the pair swapped atomically.

    Fail: Adds more atomics, or makes both volatile.

Score yourself

9/9 — you can defend this in a senior round under pushback 7-8 — solid; ordering and the testing question are where points go 5-6 — you know volatile is not atomic but not the mechanism; redo the Challenge 0-4 — run the Warm-up and watch the counter lose two thirds of its work

← Back to What does volatile guarantee, and what does it not?