ByteDance · Low-Level Design
Design a C++ Shared-Ownership Pointer Library
TrueInterview
October 7, 2026 · 1 min read
Design a small C++ shared-ownership pointer library. Explain the class interface, how ownership is represented, copy and move behavior, and the conditions under which the managed object is destroyed.
Requirements and Constraints
The task is to implement shared-pointer behavior, but the exact library surface is not specified. For a bounded exercise, assume single-threaded ownership of one object allocated for the matching type, with construction, copying, moving, assignment, reset, dereference, and an observer for the ownership count. State this scope explicitly. Weak references, array ownership, custom deleters, aliasing constructors, and thread-safe reference counts are extensions to clarify, not requirements to assume silently.
Clarifying Questions
- Which constructors and observers are required, and how is an empty pointer represented?
- Must copies share one control block, and what guarantees should assignment provide if allocation fails?
- Is concurrent access in scope, and if so, does it refer to the control block, the same pointer object, or the pointed-to object?
- Are cycles or weak references part of the required behavior?
Follow-up Questions
- Why can two independently constructed owners of the same raw pointer cause a double deletion?
- How does a move differ from a copy in its effect on the reference count?
- Why would atomic reference counts alone not make concurrent mutation of the pointed-to object safe?