Implement a versioned hash map with snapshot
Versioned Hash Map with Snapshot
Design and implement a data structure, VersionedHashMap<K, V>, that extends the functionality of a standard hash map to support time-travel queries and snapshot creation.
Requirements
Implement the following methods:
-
long put(K key, V value)- Stores the given
valuefor the specifiedkey. - Returns a
timestamprepresenting the exact moment this write operation occurred.
- Stores the given
-
V get(K key)- Retrieves the latest value currently associated with
key. - If
keyhas never been set, returnsnull.
- Retrieves the latest value currently associated with
-
V get(K key, long timestamp)- Retrieves the value associated with
keyas of the giventimestamp. - This means returning the value from the write operation with the greatest write-timestamp
tsuch thatt <= timestamp. - If no write for
keyoccurred at or beforetimestamp, returnsnull.
- Retrieves the value associated with
-
Map<K, V> freeze(long timestamp)- Generates and returns a plain (non-versioned)
Map<K, V>that represents a snapshot of the entireVersionedHashMap's state as of the specifiedtimestamp. - For each key that existed at or before
timestamp, the returned map should contain its latest value as of thattimestamp.
- Generates and returns a plain (non-versioned)
Notes and Assumptions
- Timestamps returned by
putare guaranteed to be comparable and strictly increasing for successiveputcalls. This could be an internal counter or a monotonic millisecond timestamp. - A
freeze(timestamp)operation should produce a snapshot that is immutable with respect to futureputoperations; subsequent writes should not alter previously frozen snapshots. - Deletion functionality is not a core requirement. You may choose to support it if it naturally fits your design, but it's not mandatory.
Example
Consider the following sequence of operations and their resulting timestamps:
put("a", 10) -> 1put("a", 20) -> 2put("b", 7) -> 3
Based on these operations:
get("a", 1)should return10.get("a", 2)should return20.get("a", 100)should return20(as 20 is the latest value up to timestamp 100).freeze(2)should return{ "a": 20 }.freeze(3)should return{ "a": 20, "b": 7 }.
Additional Considerations
Be prepared to discuss any additional constraints, such as expected time and space complexity for operations, and concurrency expectations, if applicable to your design.
AI interview prep
Core dilemma
Balancing efficient storage and retrieval of historical data versions for individual keys against the performance and memory implications of generating full-map snapshots.
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 versioned hash map with snapshot
Versioned Hash Map with Snapshot
Design and implement a data structure, VersionedHashMap<K, V>, that extends the functionality of a standard hash map to support time-travel queries and snapshot creation.
Requirements
Implement the following methods:
-
long put(K key, V value)- Stores the given
valuefor the specifiedkey. - Returns a
timestamprepresenting the exact moment this write operation occurred.
- Stores the given
-
V get(K key)- Retrieves the latest value currently associated with
key. - If
keyhas never been set, returnsnull.
- Retrieves the latest value currently associated with
-
V get(K key, long timestamp)- Retrieves the value associated with
keyas of the giventimestamp. - This means returning the value from the write operation with the greatest write-timestamp
tsuch thatt <= timestamp. - If no write for
keyoccurred at or beforetimestamp, returnsnull.
- Retrieves the value associated with
-
Map<K, V> freeze(long timestamp)- Generates and returns a plain (non-versioned)
Map<K, V>that represents a snapshot of the entireVersionedHashMap's state as of the specifiedtimestamp. - For each key that existed at or before
timestamp, the returned map should contain its latest value as of thattimestamp.
- Generates and returns a plain (non-versioned)
Notes and Assumptions
- Timestamps returned by
putare guaranteed to be comparable and strictly increasing for successiveputcalls. This could be an internal counter or a monotonic millisecond timestamp. - A
freeze(timestamp)operation should produce a snapshot that is immutable with respect to futureputoperations; subsequent writes should not alter previously frozen snapshots. - Deletion functionality is not a core requirement. You may choose to support it if it naturally fits your design, but it's not mandatory.
Example
Consider the following sequence of operations and their resulting timestamps:
put("a", 10) -> 1put("a", 20) -> 2put("b", 7) -> 3
Based on these operations:
get("a", 1)should return10.get("a", 2)should return20.get("a", 100)should return20(as 20 is the latest value up to timestamp 100).freeze(2)should return{ "a": 20 }.freeze(3)should return{ "a": 20, "b": 7 }.
Additional Considerations
Be prepared to discuss any additional constraints, such as expected time and space complexity for operations, and concurrency expectations, if applicable to your design.