Concurrency, Scheduling, and State Machines
Asked of: Software Engineer
Last updated
What's being tested
These problems test building and reasoning about an async primitive (like CompletableFuture) and scheduling: safe callback registration, correct completion state machine, and efficient timer/task multiplexing onto a thread-pool. Interviewers probe race-free state transitions, cancellation, ordering guarantees, and scalable timer/scheduler data structures.
Patterns & templates
-
State machine with explicit states (e.g., PENDING → COMPLETING → COMPLETED/FAILED/CANCELLED) and a single CAS transition per completion to avoid races.
-
Callback list appended atomically with
compareAndSetand drained by the thread that wins completion, avoiding locking on hot path. -
Use volatile for result/state visibility and minimal locking; prefer lock-free for low-latency futures, fallback to short critical sections when necessary.
-
Implement timers with a min-heap priority queue for correctness, or a timer wheel for large-scale (~10^6 timers) amortized O(1) ticks.
-
Use ForkJoinPool / work-stealing for parallel array processing; prefer divide-and-conquer recursively to maximize locality and CPU utilization.
-
Cancellation: mark state then attempt to remove scheduled tasks; for heap-based timers, use lazy deletion flags to avoid O(n) removals.
-
Batch wakeups: coalesce scheduled callbacks and run them on a worker thread (
execute()), avoid running user callbacks under internal locks to prevent deadlocks.
Common pitfalls
Pitfall: Failing to make state transitions atomic leads to double-completion or lost callbacks when two threads try to complete concurrently.
Pitfall: Running user callbacks while holding internal locks causes deadlocks or long GC pauses; always invoke callbacks outside locks.
Pitfall: Assuming
notify()without rechecking condition — handle spurious wakeups and always loop on the predicate when usingwait()/notify().
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Practice questions
- Implement Multi-Player Tic-Tac-ToeGoogle · Software Engineer · Onsite · medium
- Build a Custom CompletableFuture: Async Primitive and Parallel Array ProcessingGoogle · Software Engineer · Technical Screen · medium
- Design a restaurant waitlist systemGoogle · Software Engineer · Technical Screen · medium
- Design Tic-Tac-Toe With K PlayersGoogle · Software Engineer · Technical Screen · medium
- Implement Batched Undo/Redo LayerGoogle · Software Engineer · Technical Screen · easy
- Implement a Reusable Locker Allocation SystemGoogle · Software Engineer · Technical Screen · medium
- Design a multi-timer using single underlying timerGoogle · Software Engineer · Online Assessment · medium
- Design an elevator control systemGoogle · Software Engineer · Technical Screen · medium
- Design task scheduler with dependenciesGoogle · Software Engineer · Technical Screen · hard
- Design an elevator control systemGoogle · Software Engineer · Onsite · hard
- Design a Collaborative Notes ServiceGoogle · Software Engineer · Onsite · hard
Related concepts
- Concurrency, OS Threads, And MemorySoftware Engineering Fundamentals
- Concurrency, Deadlocks, And SynchronizationSoftware Engineering Fundamentals
- Concurrency And SynchronizationSoftware Engineering Fundamentals
- Thread-Safe Queues And Concurrency PrimitivesCoding & Algorithms
- Intervals, Sliding Windows, And Time-Ordered StateCoding & Algorithms
- Concurrency Control And Thread SafetySystem Design