Implement a Fixed-Size Thread Pool With Futures and Graceful Shutdown

Last updated: November 29, 2026

Quick Overview

Build a thread pool with a fixed number of workers that accepts tasks, returns a future for each, propagates exceptions to the caller, and shuts down cleanly after draining queued work.

Google
Software Engineering Fundamentals
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Software Engineering Fundamentals
Hard

0

0

0 solved


Build a thread pool with a fixed number of workers that accepts tasks, returns a future for each, propagates exceptions to the caller, and shuts down cleanly after draining queued work.

Google onsite fundamentals rounds like asking candidates to build a primitive that production code at Google relies on (executors back almost every RPC server). The interviewer watches for correct condition-variable usage, running tasks outside the lock, exception propagation, and a shutdown that neither drops work nor hangs.

What the Interviewer Expects
  • Uses a condition variable with a while-loop wait to handle spurious wakeups
  • Executes user tasks outside the lock
  • Captures exceptions in the future instead of killing the worker
  • Shutdown drains queued tasks, wakes all workers, and rejects new submissions
  • Discusses bounded versus unbounded queues and back-pressure
Key Topics to Cover
condition variable
producer-consumer
futures
graceful shutdown
exception propagation
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • How would you add `shutdown_now` that cancels tasks still in the queue?
  • How do you bound the queue and what policy applies when it is full?
  • How would you detect a task that never returns?
  • What deadlock appears if a task submits to the same pool and waits on the result?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice System Design Problems
Sample Answer
Requirements
  • submit(fn, *args) returns a Future immediately; future.result(timeout) blocks until done and re-raises the task's exception.
  • Exactly workers threads run tasks.
  • shutdown(wait=True): stop ...
High-Level Design

A deque of (fn, args, future) guarded by one Condition. Workers loop: wait while the queue is empty and not shut down; if shut down and empty, exit; otherwise pop and run the task with the lock re...


Submit Your Answer
Markdown supported