← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Interviewed at Google for a software engineering role, got a concurrency question that sounds deceptively simple but has a lot of nuance underneath.

Questions Asked (1)

Q1

When would you choose volatile over synchronized in Java?

Technical Trade-offsSystem Design
Author's notes

I knew the textbook answer but fumbled explaining the actual tradeoff under pressure.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the distinct guarantees each provides: volatile ensures visibility and ordering for a single variable, while synchronized provides mutual exclusion and visibility for a block of code. Then explain that volatile is chosen when you need lightweight, lock-free visibility for a single variable without compound actions, and synchronized when you need atomicity for compound operations or coordination across multiple variables.

Pro tip: Emphasize that volatile is not a replacement for synchronized because it doesn't provide atomicity for compound actions like check-then-act; mention that in practice, you often combine volatile with other constructs (e.g., for flags or one-time initialization) and that java.util.concurrent utilities often supersede both.

1. Define the guarantees

Briefly state that volatile guarantees visibility and ordering for a single variable, while synchronized guarantees mutual exclusion and visibility for a block of code.

2. Identify the need

Determine whether the requirement is only visibility for a single variable (use volatile) or atomicity for compound actions or multiple variables (use synchronized).

3. Consider performance and contention

Note that volatile is lighter-weight and non-blocking, while synchronized can cause contention and context switching, so volatile is preferable when it suffices.

4. Provide concrete examples

Give scenarios: volatile for a status flag or one-time initialization; synchronized for incrementing a counter or updating multiple related fields.

5. Mention alternatives and best practices

Acknowledge that java.util.concurrent classes (e.g., AtomicInteger) often replace both, and that volatile is not a substitute for synchronized when atomicity is needed.

Key Points to Mention

  • Volatile ensures visibility and prevents reordering but does not provide atomicity for compound actions (e.g., i++).
  • Synchronized provides both mutual exclusion and visibility, allowing atomic execution of a block.
  • Use volatile for simple flags or one-time initialization where only visibility is required.
  • Use synchronized when multiple variables must be updated atomically or when check-then-act sequences are needed.
  • Volatile is generally more scalable and avoids deadlocks, but synchronized is necessary for complex state consistency.
  • Java's java.util.concurrent package offers higher-level abstractions (e.g., AtomicInteger, ReentrantLock) that often supersede both.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.