Katkat

Fix and harden an object pool

Akuna CapitalSoftware Engineer · Online Assessment

Last updated

This interview question evaluates requirements, scale assumptions, API/data design, architecture, trade-offs, failure modes, and rollout in a realistic interview setting. A strong answer for Fix and harden an object pool states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Fix a buggy C++ object pool implementation. The current code exhibits races, double-free risks, and resource leaks under concurrent borrow/return. Refactor it into a correct, thread-safe object pool template.

Requirements:

  • Provide borrow() that blocks (with an optional timeout) when the pool is empty, creating up to a max capacity; ensure fairness and avoid spurious wakeups.
  • Provide returnObject(obj) that safely returns an object; reject invalid or duplicate returns; optionally reset objects before reuse.
  • Enforce RAII with a scoped handle that automatically returns the object on destruction; handle must be movable but not copyable.
  • Ensure safe shutdown: destructor stops producers/consumers, wakes waiters, and releases resources without deadlocks.
  • Avoid busy-waiting; use std::mutex, std::condition_variable, and std::atomic correctly; prevent ABA issues (e.g., with generation counters).
  • Include minimal unit tests demonstrating correctness under multiple threads.

Assume C++17 or later and document the complexity of core operations.