Implement and Explain a C++ Smart Pointer
Company: NVIDIA
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Technical Screen
# Implement and Explain a C++ Smart Pointer
The preserved report says only to implement a C++ smart pointer; it does not identify unique, shared, weak, or intrusive ownership. Begin by clarifying the ownership model and required interface. If the interviewer selects unique ownership, implement a minimal scalar-object owner and explain how the design would differ for shared ownership.
### Constraints & Assumptions
- Unique ownership is an explicit practice branch, not a source-preserved requirement.
- In that branch, the pointer owns one object created for scalar `delete`, may be null, and must not use `std::unique_ptr` internally.
- Copy, move, deleter, array, and incomplete-type behavior must follow the ownership model the interviewer confirms.
- A shared-ownership branch would require a control block and different copy semantics.
### Clarifying Questions to Ask
- Is ownership unique, shared, weak, or intrusive?
- Which constructors, observers, modifiers, and conversions are required?
- Are arrays, custom deleters, derived-to-base conversion, or incomplete types in scope?
- What thread-safety and exception guarantees are expected?
### Part 1 — Fix the ownership contract
State who owns the object, which operations transfer or share ownership, and exactly when deletion occurs.
#### What This Part Should Cover
- Ownership model and copy or move behavior
- Null and destruction semantics
- Deleter and scalar-versus-array policy
- Required interface and exception guarantees
### Part 2 — Implement one confirmed branch
For the illustrative unique-ownership branch, implement construction, destruction, deleted copies, moves, `get`, dereference, member access, Boolean conversion, `release`, `reset`, and `swap`.
#### What This Part Should Cover
- One raw pointer with one accountable owner
- `noexcept` move and swap
- Correct `release`, `reset`, and self-operation behavior
- Const-correct observers
```hint Define ownership before methods
A method list cannot determine whether copying transfers, shares, or forbids ownership; settle that invariant first.
```
### What a Strong Answer Covers
- Recognition that “smart pointer” alone is not a complete ownership contract
- A correct implementation for the explicitly selected model
- Precise destruction, transfer, reset, and null semantics
- Differences among unique ownership, reference counting, arrays, and custom deleters
### Follow-up Questions
1. What additional state and atomic behavior does shared ownership require?
2. Why can deleting a derived object through a base pointer be unsafe?
3. How would an array specialization differ?
Overview: Clarify the ownership model behind a C++ smart-pointer prompt, then implement and reason about an explicitly selected unique- or shared-ownership contract.