Netflix Coding: Bounded Blocking Queue Implementation
Question Description
Question overview
You must implement a thread-safe, bounded blocking queue with a fixed positive integer capacity supplied at construction. You are expected to provide offer(item) that blocks when the queue is full until space is available, poll() that blocks when the queue is empty until an element arrives and returns the removed head, peek() that is non-blocking and returns the head or None if empty, and size() that returns the current count. Synchronization should use explicit wait/signal (condition variable) style primitives.
How the interview will typically flow
You’ll start by clarifying capacity semantics (capacity > 0), blocking/non-blocking behavior, and whether spurious wakeups/timeouts or shutdown signals are required. Then sketch the design: a mutex/lock, an internal buffer (e.g., array or deque), and two condition variables (not_full and not_empty). Next, discuss correctness under concurrency (use while loops around waits), then implement or pseudocode the key methods and discuss edge cases.
Skills and signals the interviewer is looking for
You should demonstrate knowledge of locks and condition variables, correct use of wait in a loop to handle spurious wakeups, how to maintain invariants (0 ≤ size ≤ capacity), safe notifications (notify vs notify_all), and capacity enforcement. Mentioning alternative implementations (semaphores, ReentrantLock+Condition) and trade-offs (fairness, performance) is a plus.
This question tests core concurrency, multithreading, and concurrent data structure design—use clear reasoning about liveness and safety as you code.
Common Follow-up Questions
- •How would you add a timeout variant to offer(item, timeout) and poll(timeout)? Explain the semantics and how to implement with condition wait(timeout).
- •How do you modify the queue to support graceful shutdown so blocked producers/consumers unblock and receive an error/exception?
- •Describe how you'd enforce stronger fairness (e.g., FIFO ordering of waiting producers and consumers). What changes are required and what are the trade-offs?
- •Re-implement the queue using semaphores instead of condition variables. Compare the complexity and performance implications.
Related Questions
Explore More Questions
Practice This Question with AI
Get real-time hints, detailed requirements, and insightful analysis of the question.