Thread-Safe Queues And Concurrency Primitives
Asked of: Software Engineer
Last updated

What's being tested
This tests concurrent data structure design: implementing FIFO queues/buffers that remain correct under multiple producers and consumers. Interviewers probe whether you can use mutexes, condition variables, semaphores, and shutdown semantics without races, deadlocks, busy-waiting, or lost wakeups.
Patterns & templates
-
Bounded blocking queue — store items in
collections.deque;put()waits while full,get()waits while empty; both areO(1). -
Condition-variable loop — always call
wait()insidewhile not predicate; handles spurious wakeups, missed notifications, and predicate changes after reacquiring lock. -
Producer–consumer template — one lock protects queue state;
not_empty.notify()after enqueue,not_full.notify()after dequeue; avoid holding lock during expensive work. -
Timed waits — compute absolute deadline with
time.monotonic(); loop with remaining timeout; returnFalse,None, or raiseTimeoutErrorconsistently. -
Shutdown protocol — maintain
closedflag under the same lock; wake all waiters withnotify_all(); define whether pending items drain or abort. -
CPU vs I/O concurrency — Python threads help I/O-bound work despite the GIL; CPU-bound image processing usually needs
multiprocessingor native extensions. -
Thread-pool pipeline — use
queue.Queue, worker sentinels,join(), and exception collection; bound queue size to apply backpressure and cap memory.
Common pitfalls
Pitfall: Using
if queue_empty: wait()instead ofwhile queue_empty: wait()can break under spurious wakeups or competing consumers.
Pitfall: Calling callbacks, image transforms, network I/O, or disk writes while holding the queue lock serializes the system and risks deadlock.
Pitfall: Forgetting shutdown behavior leaves blocked producers or consumers hanging forever; explicitly wake waiters and document drain-vs-cancel semantics.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Featured in interview prep guides
Practice questions
- Implement Parallel Image ProcessingAnthropic · Software Engineer · Onsite · medium
- Explain CPU-Bound vs I/O-Bound WorkAnthropic · Software Engineer · Technical Screen · hard
- Generate outputs for images and pipelinesAnthropic · Software Engineer · Technical Screen · medium
- Design a scalable network I/O serviceAnthropic · Software Engineer · Technical Screen · hard
- Scale crawler with thread poolAnthropic · Software Engineer · Technical Screen · hard
- Implement a thread-safe producer–consumer bufferAnthropic · Software Engineer · HR Screen · hard
- Implement thread-safe blocking queueAnthropic · Software Engineer · Onsite · Medium
Related concepts
- Concurrency And Thread SafetyCoding & Algorithms
- Concurrency, Deadlocks, And SynchronizationSoftware Engineering Fundamentals
- Concurrency Control And Thread SafetySystem Design
- Concurrency And Multithreading FundamentalsCoding & Algorithms
- Java, Concurrency, And Framework InternalsSoftware Engineering Fundamentals
- Concurrency ControlSystem Design