Design a key-value store class with deep copy and shallow copy semantics

Quick Overview

Implement a key-value store class that can take a deep copy or a shallow copy of its state and report whether writes made through the copy reached the original. It tests reference and value semantics, aliasing bugs, and the time and space cost of each kind of copy.

Design a key-value store class with deep copy and shallow copy semantics

Company: NVIDIA

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design a class called `DeepCopyManager` that manages an internal key-value store, simulating an object with pointer or reference members. The class must correctly handle both deep copy and shallow copy semantics. A deep copy creates a fully independent clone of the internal state, so that modifications to the copy do not affect the original. A shallow copy shares the same underlying reference, meaning changes to the copy will also mutate the original. Your implementation should allow setting and getting values in both the original store and the copy, and provide methods to verify whether modifications to a copy have leaked into the original. Implement the following methods: - `set(key: str, value: int)`: Set a key-value pair in the original store. - `get(key: str) -> int`: Get the value for a key from the original store. Returns -1 if the key is not found. - `deepCopy()`: Create a deep copy of the original store. The copy is fully independent of the original. - `shallowCopy()`: Create a shallow copy of the original store. The copy shares the same reference as the original, so mutations to the copy affect the original. - `setInCopy(key: str, value: int)`: Set a key-value pair in the most recently created copy. - `getFromCopy(key: str) -> int`: Get the value for a key from the copy. Returns -1 if the key is not found or no copy exists. - `isOriginalAffected(key: str) -> bool`: Return true if the given key exists in the original store. Useful for verifying whether modifications to a copy leaked into the original. - `getStoreSize() -> int`: Return the number of keys in the original store. - `getCopySize() -> int`: Return the number of keys in the copy. Returns -1 if no copy exists. Example 1, a deep copy: ```text Input: ["DeepCopyManager", "set", "set", "deepCopy", "setInCopy", "get", "getFromCopy", "isOriginalAffected", "getStoreSize", "getCopySize"] [[], ["x", 100], ["y", 200], [], ["z", 300], ["z"], ["z"], ["z"], [], []] Output: [null, null, null, null, null, -1, 300, false, 2, 3] Explanation: mgr = DeepCopyManager() # initialize empty manager mgr.set("x", 100) # original: {x=100} mgr.set("y", 200) # original: {x=100, y=200} mgr.deepCopy() # copy: {x=100, y=200}, an independent clone mgr.setInCopy("z", 300) # copy: {x=100, y=200, z=300}, original unchanged mgr.get("z") # -> -1 (z never reached the original) mgr.getFromCopy("z") # -> 300 mgr.isOriginalAffected("z") # -> false mgr.getStoreSize() # -> 2 (original has x, y) mgr.getCopySize() # -> 3 (copy has x, y, z) ``` Example 2, a shallow copy: ```text Input: ["DeepCopyManager", "set", "set", "shallowCopy", "setInCopy", "get", "isOriginalAffected", "getStoreSize", "getCopySize"] [[], ["x", 100], ["y", 200], [], ["z", 300], ["z"], ["z"], [], []] Output: [null, null, null, null, null, 300, true, 3, 3] Explanation: mgr = DeepCopyManager() # initialize empty manager mgr.set("x", 100) # original: {x=100} mgr.set("y", 200) # original: {x=100, y=200} mgr.shallowCopy() # shallow copy shares same reference as original mgr.setInCopy("z", 300) # copy: {x=100, y=200, z=300}, but since shallow, original is also mutated mgr.get("z") # -> 300 (original was affected by shallow copy mutation) mgr.isOriginalAffected("z") # -> true (shallow copy leaked into original) mgr.getStoreSize() # -> 3 (original now has x, y, z) mgr.getCopySize() # -> 3 (copy also has x, y, z) ``` ```hint Names and objects In Python, assigning a dict to a second attribute does not build a new dict. Work out which operations create a new object and which only give an existing object another name. ``` ### Constraints & Assumptions - `1 <= key.length <= 100` - Keys are strings containing lowercase English letters and digits. - `-10^6 <= value <= 10^6` - At most 1000 operations will be performed. - `getFromCopy` and `setInCopy` are only called after a copy has been created. - Each call to `deepCopy` or `shallowCopy` replaces the previous copy. ### Clarifying Questions to Ask - Are values always integers, or can they be mutable objects such as lists or nested maps? The answer decides whether copying the top-level map is enough. - After a deep copy, should later writes to the original stay invisible to the copy, so the copy behaves as a snapshot? - Since -1 is a legal value, is -1 acceptable as the "not found" result, or should a missing key raise an error or return a null value? ### What a Strong Answer Covers - Which copy method allocates a new container and which only adds a second reference to the existing one, and how that produces the outputs of both examples. - Why one level of copying gives full independence for string keys and integer values, and what a deep copy has to do once values can be mutable. - Behavior at the edges: no copy yet, a new copy replacing an old one, writes to the original after each kind of copy, and the -1 sentinel colliding with a legal value. - The time and space cost of each method, especially the two copy methods. - The limits of `isOriginalAffected` as a leak check, given that it only tests whether a key is present. ### Follow-up Questions - Values can now be lists or nested dictionaries that may refer to each other. How does `deepCopy` change, and how do you handle a cycle? - Python calls `dict(d)` and `copy.copy(d)` shallow copies. How do they differ from the `shallowCopy` defined here, and when would the difference show up? - In C++, what goes wrong when a class with a raw pointer member relies on the compiler-generated copy constructor, and how do you fix it? - Deep copying a large store on every call is expensive. How would copy-on-write reduce that cost, and what does it add to each write?

Overview: Implement a key-value store class that can take a deep copy or a shallow copy of its state and report whether writes made through the copy reached the original. It tests reference and value semantics, aliasing bugs, and the time and space cost of each kind of copy.

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

Design a class called DeepCopyManager that manages an internal key-value store, simulating an object with pointer or reference members. The class must correctly handle both deep copy and shallow copy semantics.

A deep copy creates a fully independent clone of the internal state, so that modifications to the copy do not affect the original. A shallow copy shares the same underlying reference, meaning changes to the copy will also mutate the original.

Your implementation should allow setting and getting values in both the original store and the copy, and provide methods to verify whether modifications to a copy have leaked into the original.

Implement the following methods:

  • set(key: str, value: int) : Set a key-value pair in the original store.
  • get(key: str) -> int : Get the value for a key from the original store. Returns -1 if the key is not found.
  • deepCopy() : Create a deep copy of the original store. The copy is fully independent of the original.
  • shallowCopy() : Create a shallow copy of the original store. The copy shares the same reference as the original, so mutations to the copy affect the original.
  • setInCopy(key: str, value: int) : Set a key-value pair in the most recently created copy.
  • getFromCopy(key: str) -> int : Get the value for a key from the copy. Returns -1 if the key is not found or no copy exists.
  • isOriginalAffected(key: str) -> bool : Return true if the given key exists in the original store. Useful for verifying whether modifications to a copy leaked into the original.
  • getStoreSize() -> int : Return the number of keys in the original store.
  • getCopySize() -> int : Return the number of keys in the copy. Returns -1 if no copy exists.

Example 1, a deep copy:

Input:
["DeepCopyManager", "set",      "set",      "deepCopy", "setInCopy", "get", "getFromCopy", "isOriginalAffected", "getStoreSize", "getCopySize"]
[[],                ["x", 100], ["y", 200], [],         ["z", 300],  ["z"], ["z"],         ["z"],                [],             []]

Output: [null, null, null, null, null, -1, 300, false, 2, 3]

Explanation:
mgr = DeepCopyManager()          # initialize empty manager
mgr.set("x", 100)                # original: {x=100}
mgr.set("y", 200)                # original: {x=100, y=200}
mgr.deepCopy()                   # copy: {x=100, y=200}, an independent clone
mgr.setInCopy("z", 300)          # copy: {x=100, y=200, z=300}, original unchanged
mgr.get("z")                     # -> -1  (z never reached the original)
mgr.getFromCopy("z")             # -> 300
mgr.isOriginalAffected("z")      # -> false
mgr.getStoreSize()               # -> 2   (original has x, y)
mgr.getCopySize()                # -> 3   (copy has x, y, z)

Example 2, a shallow copy:

Input:
["DeepCopyManager", "set",      "set",      "shallowCopy", "setInCopy", "get", "isOriginalAffected", "getStoreSize", "getCopySize"]
[[],                ["x", 100], ["y", 200], [],            ["z", 300],  ["z"], ["z"],                [],             []]

Output: [null, null, null, null, null, 300, true, 3, 3]

Explanation:
mgr = DeepCopyManager()          # initialize empty manager
mgr.set("x", 100)                # original: {x=100}
mgr.set("y", 200)                # original: {x=100, y=200}
mgr.shallowCopy()                # shallow copy shares same reference as original
mgr.setInCopy("z", 300)          # copy: {x=100, y=200, z=300}, but since shallow, original is also mutated
mgr.get("z")                     # -> 300  (original was affected by shallow copy mutation)
mgr.isOriginalAffected("z")      # -> true (shallow copy leaked into original)
mgr.getStoreSize()               # -> 3   (original now has x, y, z)
mgr.getCopySize()                # -> 3   (copy also has x, y, z)

Constraints & Assumptions

  • 1 <= key.length <= 100
  • Keys are strings containing lowercase English letters and digits.
  • -10^6 <= value <= 10^6
  • At most 1000 operations will be performed.
  • getFromCopy and setInCopy are only called after a copy has been created.
  • Each call to deepCopy or shallowCopy replaces the previous copy.

Clarifying Questions to Ask Guidance

  • Are values always integers, or can they be mutable objects such as lists or nested maps? The answer decides whether copying the top-level map is enough.
  • After a deep copy, should later writes to the original stay invisible to the copy, so the copy behaves as a snapshot?
  • Since -1 is a legal value, is -1 acceptable as the "not found" result, or should a missing key raise an error or return a null value?

What a Strong Answer Covers Guidance

  • Which copy method allocates a new container and which only adds a second reference to the existing one, and how that produces the outputs of both examples.
  • Why one level of copying gives full independence for string keys and integer values, and what a deep copy has to do once values can be mutable.
  • Behavior at the edges: no copy yet, a new copy replacing an old one, writes to the original after each kind of copy, and the -1 sentinel colliding with a legal value.
  • The time and space cost of each method, especially the two copy methods.
  • The limits of isOriginalAffected as a leak check, given that it only tests whether a key is present.

Follow-up Questions Guidance

  • Values can now be lists or nested dictionaries that may refer to each other. How does deepCopy change, and how do you handle a cycle?
  • Python calls dict(d) and copy.copy(d) shallow copies. How do they differ from the shallowCopy defined here, and when would the difference show up?
  • In C++, what goes wrong when a class with a raw pointer member relies on the compiler-generated copy constructor, and how do you fix it?
  • Deep copying a large store on every call is expensive. How would copy-on-write reduce that cost, and what does it add to each write?