Design a shared pointer manager with reference counting

Quick Overview

Design a manager for named shared pointers to integer resources, with create, copy, assign, release, dereference and reference count queries. It tests reference counting, shared ownership through one resource object, the ideas behind RAII, and careful handling of rebinding and self-copy edge cases.

Design a shared pointer manager with reference counting

Company: NVIDIA

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a shared pointer manager that demonstrates the principles of RAII (Resource Acquisition Is Initialization) and reference counting. The manager tracks named pointers to integer resources. Multiple pointers can share ownership of the same resource. Each resource keeps a reference count of how many pointers refer to it. When the last pointer to a resource is released or reassigned, the resource is considered destroyed (its reference count reaches zero). The manager should support creating new pointers, copying pointers to share ownership, reassigning pointers to new resources, releasing pointers, dereferencing to retrieve values, and querying the current reference count. Implement the following methods: - `create(name: str, value: int)`: Create a new shared pointer with the given name pointing to a new resource with the given value. If the name already exists, the old resource is released first. - `copy(source: str, dest: str) -> bool`: Copy the shared pointer from `source` to `dest`. Both share ownership of the same resource (its reference count increments). Returns `True` if successful, `False` if `source` does not exist. If `dest` already exists, its old resource is released first. - `assign(name: str, value: int) -> bool`: Reassign the pointer to a brand new resource with the given value. The old resource's reference count is decremented. Returns `True` if the pointer existed, `False` otherwise. - `release(name: str) -> bool`: Release the pointer, decrementing the reference count of the underlying resource. Returns `True` if the pointer existed, `False` otherwise. - `dereference(name: str) -> int`: Get the value the pointer points to. Returns `-1` if the pointer does not exist. - `getRefCount(name: str) -> int`: Get the reference count of the resource the named pointer points to. Returns `0` if the pointer does not exist. Example 1: ```text Input: ["SharedPointerManager", "create", "copy", "getRefCount", "getRefCount", "copy", "assign", "getRefCount", "dereference", "dereference", "release", "getRefCount", "copy"] [[], ["a", 5], ["a", "b"], ["a"], ["b"], ["a", "c"], ["b", 7], ["a"], ["b"], ["c"], ["a"], ["c"], ["z", "a"]] Output: [null, null, true, 2, 2, true, true, 2, 7, 5, true, 1, false] Explanation: mgr = SharedPointerManager() mgr.create("a", 5) # a -> R1 [5, ref=1] mgr.copy("a", "b") # -> true, a and b share R1 [5, ref=2] mgr.getRefCount("a") # -> 2 mgr.getRefCount("b") # -> 2 mgr.copy("a", "c") # -> true, R1 [5, ref=3] mgr.assign("b", 7) # -> true, R1 drops to ref=2, b -> R2 [7, ref=1] mgr.getRefCount("a") # -> 2 (a and c) mgr.dereference("b") # -> 7 mgr.dereference("c") # -> 5 mgr.release("a") # -> true, R1 [5, ref=1], only c holds it mgr.getRefCount("c") # -> 1 mgr.copy("z", "a") # -> false, z does not exist ``` Example 2: ```text Input: ["SharedPointerManager", "create", "dereference", "assign", "dereference", "release", "dereference", "release"] [[], ["x", 10], ["x"], ["x", 20], ["x"], ["x"], ["x"], ["x"]] Output: [null, null, 10, true, 20, true, -1, false] Explanation: mgr = SharedPointerManager() # initialize manager mgr.create("x", 10) # x -> [10, ref=1] mgr.dereference("x") # -> 10 mgr.assign("x", 20) # -> true, old resource destroyed, x -> [20, ref=1] mgr.dereference("x") # -> 20 mgr.release("x") # -> true, resource destroyed mgr.dereference("x") # -> -1 (x no longer exists) mgr.release("x") # -> false (x does not exist) ``` ```hint Identity or value After a copy, a change to the reference count made through one name has to be visible through the other. Think about what the two names must hold for that to happen. ``` ```hint Rebinding a name in use `create`, `copy` and `assign` can all point a name somewhere new while it still holds a resource. Decide what happens to the old resource first, and check what your order does for `copy("a", "a")`. ``` ### Constraints & Assumptions - `1 <= name.length <= 20` - Names consist of lowercase English letters and digits. - `-10^6 <= value <= 10^6` - At most `10^4` operations will be made. - `dereference` returns `-1` for a pointer that does not exist, and `getRefCount` returns `0`. - Copying a pointer onto itself (`source == dest`) returns `True` and changes nothing. ### Clarifying Questions to Ask - Since `value` can be `-1`, is the `-1` result from `dereference` an acceptable way to signal a missing pointer, or should callers have another way to tell the cases apart? - Does anything need to happen when a resource is destroyed (a callback, a log line, a counter), or is reaching a count of zero enough? - Will the manager be called from more than one thread? ### What a Strong Answer Covers - Shared ownership through one resource object referenced by several names, so a count change is visible from all of them - Releasing the old resource whenever `create`, `copy` or `assign` rebinds a name that already holds one - Edge cases: missing names, copying a name onto itself, and copying onto a name that already shares the resource - How a count of zero corresponds to destruction, and how the operations map to RAII and `std::shared_ptr` - The cost of each operation ### Follow-up Questions - How would you add weak pointers that can observe a resource without keeping it alive? - What has to change for the counts to stay correct when several threads copy and release pointers at the same time? - If resources could hold pointers to other resources, how would a cycle affect destruction, and how would you deal with it?

Overview: Design a manager for named shared pointers to integer resources, with create, copy, assign, release, dereference and reference count queries. It tests reference counting, shared ownership through one resource object, the ideas behind RAII, and careful handling of rebinding and self-copy edge cases.

NVIDIA logo
NVIDIA
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a shared pointer manager that demonstrates the principles of RAII (Resource Acquisition Is Initialization) and reference counting. The manager tracks named pointers to integer resources.

Multiple pointers can share ownership of the same resource. Each resource keeps a reference count of how many pointers refer to it. When the last pointer to a resource is released or reassigned, the resource is considered destroyed (its reference count reaches zero).

The manager should support creating new pointers, copying pointers to share ownership, reassigning pointers to new resources, releasing pointers, dereferencing to retrieve values, and querying the current reference count.

Implement the following methods:

  • create(name: str, value: int) : Create a new shared pointer with the given name pointing to a new resource with the given value. If the name already exists, the old resource is released first.
  • copy(source: str, dest: str) -> bool : Copy the shared pointer from source to dest . Both share ownership of the same resource (its reference count increments). Returns True if successful, False if source does not exist. If dest already exists, its old resource is released first.
  • assign(name: str, value: int) -> bool : Reassign the pointer to a brand new resource with the given value. The old resource's reference count is decremented. Returns True if the pointer existed, False otherwise.
  • release(name: str) -> bool : Release the pointer, decrementing the reference count of the underlying resource. Returns True if the pointer existed, False otherwise.
  • dereference(name: str) -> int : Get the value the pointer points to. Returns -1 if the pointer does not exist.
  • getRefCount(name: str) -> int : Get the reference count of the resource the named pointer points to. Returns 0 if the pointer does not exist.

Example 1:

Input:
["SharedPointerManager", "create", "copy", "getRefCount", "getRefCount", "copy", "assign", "getRefCount", "dereference", "dereference", "release", "getRefCount", "copy"]
[[], ["a", 5], ["a", "b"], ["a"], ["b"], ["a", "c"], ["b", 7], ["a"], ["b"], ["c"], ["a"], ["c"], ["z", "a"]]

Output: [null, null, true, 2, 2, true, true, 2, 7, 5, true, 1, false]

Explanation:
mgr = SharedPointerManager()
mgr.create("a", 5)        # a -> R1 [5, ref=1]
mgr.copy("a", "b")        # -> true, a and b share R1 [5, ref=2]
mgr.getRefCount("a")      # -> 2
mgr.getRefCount("b")      # -> 2
mgr.copy("a", "c")        # -> true, R1 [5, ref=3]
mgr.assign("b", 7)        # -> true, R1 drops to ref=2, b -> R2 [7, ref=1]
mgr.getRefCount("a")      # -> 2 (a and c)
mgr.dereference("b")      # -> 7
mgr.dereference("c")      # -> 5
mgr.release("a")          # -> true, R1 [5, ref=1], only c holds it
mgr.getRefCount("c")      # -> 1
mgr.copy("z", "a")        # -> false, z does not exist

Example 2:

Input:
["SharedPointerManager", "create",  "dereference", "assign",  "dereference", "release", "dereference", "release"]
[[],                     ["x", 10], ["x"],         ["x", 20], ["x"],         ["x"],     ["x"],         ["x"]]

Output: [null, null, 10, true, 20, true, -1, false]

Explanation:
mgr = SharedPointerManager()   # initialize manager
mgr.create("x", 10)            # x -> [10, ref=1]
mgr.dereference("x")           # -> 10
mgr.assign("x", 20)            # -> true, old resource destroyed, x -> [20, ref=1]
mgr.dereference("x")           # -> 20
mgr.release("x")               # -> true, resource destroyed
mgr.dereference("x")           # -> -1 (x no longer exists)
mgr.release("x")               # -> false (x does not exist)

Constraints & Assumptions

  • 1 <= name.length <= 20
  • Names consist of lowercase English letters and digits.
  • -10^6 <= value <= 10^6
  • At most 10^4 operations will be made.
  • dereference returns -1 for a pointer that does not exist, and getRefCount returns 0 .
  • Copying a pointer onto itself ( source == dest ) returns True and changes nothing.

Clarifying Questions to Ask Guidance

  • Since value can be -1 , is the -1 result from dereference an acceptable way to signal a missing pointer, or should callers have another way to tell the cases apart?
  • Does anything need to happen when a resource is destroyed (a callback, a log line, a counter), or is reaching a count of zero enough?
  • Will the manager be called from more than one thread?

What a Strong Answer Covers Guidance

  • Shared ownership through one resource object referenced by several names, so a count change is visible from all of them
  • Releasing the old resource whenever create , copy or assign rebinds a name that already holds one
  • Edge cases: missing names, copying a name onto itself, and copying onto a name that already shares the resource
  • How a count of zero corresponds to destruction, and how the operations map to RAII and std::shared_ptr
  • The cost of each operation

Follow-up Questions Guidance

  • How would you add weak pointers that can observe a resource without keeping it alive?
  • What has to change for the counts to stay correct when several threads copy and release pointers at the same time?
  • If resources could hold pointers to other resources, how would a cycle affect destruction, and how would you deal with it?