Implement a Reusable Barrier for Lockstep MapReduce Phases
Last updated: November 29, 2026
Quick Overview
Write a cyclic barrier that blocks a fixed number of worker threads until all of them arrive, optionally runs an action once per round, and can be reused for the next phase without stragglers slipping through.
0
0
0 solved
Write a cyclic barrier that blocks a fixed number of worker threads until all of them arrive, optionally runs an action once per round, and can be reused for the next phase without stragglers slipping through.
Batch pipelines in the MapReduce and Flume tradition run workers in phases where nobody may start the reduce step until every mapper finishes. Google interviewers use the reusable barrier to see whether the candidate spots the generation problem: a fast thread looping back into the barrier before slow threads have left it.
What the Interviewer Expects
- Counts arrivals under a lock and releases all on the last arrival
- Uses a generation number so the barrier is safely reusable
- Runs the per-round action exactly once, by the last arriving thread
- Waits in a loop on the generation predicate
- Discusses what should happen if a thread dies or times out
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- Add a timeout that breaks the barrier and raises in all waiting threads.
- How does a countdown latch differ, and when would you prefer it?
- How would you build a barrier across processes or machines?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
CyclicBarrier(parties, action);wait()blocks untilpartiesthreads have called it in the current round.- The last arriver runs
actiononce, then everyone proceeds. - Reusable for any numbe...
High-Level Design
A naive barrier waits on count < parties and resets count to zero on release. The bug: after reset, a released thread that is still waking up re-checks count < parties, sees it true again, and g...