Implement a thread-safe producer–consumer buffer evaluates requirements, scale assumptions, API/data design, architecture, trade-offs, failure modes, and rollout in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Design and implement a thread-safe, fixed-capacity producer–consumer buffer supporting multiple producers and consumers. Provide put(item) that blocks when full and take() that blocks when empty; preserve FIFO order; allow a configurable capacity N; add shutdown() to unblock waiting threads and reject new puts; and support timeouts for put and take. Explain how you prevent race conditions, deadlocks, and lost wakeups; discuss fairness and performance trade-offs; and analyze time and space complexity. If time permits, compare a lock-based design (mutex + condition variables) to a lock-free approach.
Quick Answer: Implement a thread-safe producer–consumer buffer evaluates requirements, scale assumptions, API/data design, architecture, trade-offs, failure modes, and rollout in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.