Sign in to track progress, unlock prep layers, and browse your pipeline.Sign in
Implement a reference-counted smart pointer
Problem Description
Implement a minimal C++ reference-counted smart pointer, SharedPtr<T>, analogous to std::shared_ptr.
Requirements
- The
SharedPtr<T>class template must manage a heap-allocatedT*. - Adhere to the Resource Acquisition Is Initialization (RAII) principle: the managed object must be deleted exactly once when the last
SharedPtrinstance owning it is destroyed. - The implementation must support the following member functions and operators:
- Default constructor (initializes to a null pointer).
- Constructor from a raw pointer
T*. - Destructor.
- Copy constructor and copy assignment operator (must correctly increase the reference count).
- Move constructor and move assignment operator (must transfer ownership, leaving the source
SharedPtrin a null state). - Dereference operator:
T& operator*(). - Member access operator:
T* operator->(). - Raw pointer accessor:
T* get() const. - Reference count accessor:
size_t use_count() const. - Reset function:
void reset(T* p = nullptr).
- The implementation must correctly handle self-assignment and prevent memory leaks or double-free issues.
Scope & Assumptions
- Thread-safety may be ignored unless explicitly chosen for implementation.
AI interview prep
Core dilemma
Ensuring deterministic and correct resource deallocation in the face of shared ownership, specifically avoiding memory leaks and double-frees, while maintaining efficient object access and minimal overhead.
Mid-level signals3
Senior signals3
Red flags3
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement a reference-counted smart pointer
Problem Description
Implement a minimal C++ reference-counted smart pointer, SharedPtr<T>, analogous to std::shared_ptr.
Requirements
- The
SharedPtr<T>class template must manage a heap-allocatedT*. - Adhere to the Resource Acquisition Is Initialization (RAII) principle: the managed object must be deleted exactly once when the last
SharedPtrinstance owning it is destroyed. - The implementation must support the following member functions and operators:
- Default constructor (initializes to a null pointer).
- Constructor from a raw pointer
T*. - Destructor.
- Copy constructor and copy assignment operator (must correctly increase the reference count).
- Move constructor and move assignment operator (must transfer ownership, leaving the source
SharedPtrin a null state). - Dereference operator:
T& operator*(). - Member access operator:
T* operator->(). - Raw pointer accessor:
T* get() const. - Reference count accessor:
size_t use_count() const. - Reset function:
void reset(T* p = nullptr).
- The implementation must correctly handle self-assignment and prevent memory leaks or double-free issues.
Scope & Assumptions
- Thread-safety may be ignored unless explicitly chosen for implementation.