Build a Bounded Frame Buffer Between Camera Capture and Encoders
Last updated: November 29, 2026
Quick Overview
Implement a thread-safe bounded buffer that a camera capture thread fills with frames and several encoder threads drain, with blocking put/get, timeouts, and a clean shutdown.
Apple
November 29, 20260
0
0 solved
Implement a thread-safe bounded buffer that a camera capture thread fills with frames and several encoder threads drain, with blocking put/get, timeouts, and a clean shutdown.
Camera and media teams at Apple care about pipelines where a producer runs at sensor rate and consumers run at encoder rate. The interviewer is checking whether you can use a lock with condition variables correctly, avoid lost wakeups, and shut the pipeline down without hanging threads.
What the Interviewer Expects
- Uses one mutex with two condition variables (or one) and waits in a loop on the predicate
- Handles timeouts so the capture thread can drop frames instead of blocking forever
- Implements close() that wakes every waiter and lets consumers drain remaining frames
- Explains why notify must happen while holding the lock and why spurious wakeups matter
- Tests with multiple consumers and verifies no frame is lost or duplicated
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
- Capture must never block: how would you change put() to drop the oldest frame instead?
- How would you implement this lock-free as a single-producer single-consumer ring buffer?
- How does this map to GCD or Swift concurrency (DispatchSemaphore, AsyncStream with a buffering policy)?
- How would you measure whether the buffer size is causing dropped frames?
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
put(frame, timeout)blocks while full; returns False on timeout so capture can drop a frame.get(timeout)blocks while empty; returns None on timeout or after close and drain.close()wake...
High-Level Design
A deque guarded by a single Lock, with two Condition objects sharing that lock: not_full for producers and not_empty for consumers. Using two conditions avoids waking producers when only con...