Time Based Key-Value Store - Complete Solution Guide
Time Based Key-Value Store is LeetCode problem 981, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key's value at a certain timestamp. Implement the TimeMap class: TimeMap() Initializes the object of the data structure. void set(String key, String value, int timestamp) Stores the key key with the value value at the given time timestamp . String get(String key, int timestamp) Returns a value such that set was called previously, with timestamp_prev <= timestamp
Detailed Explanation
The problem requires designing a data structure, `TimeMap`, that stores key-value pairs with timestamps. The `set` operation allows associating a key with a value at a specific timestamp. The `get` operation retrieves the value associated with a given key and timestamp. If an exact match for the timestamp doesn't exist, the `get` operation should return the value associated with the largest timestamp that is less than or equal to the given timestamp. If no such value exists (e.g., the timestamp is earlier than any timestamps for the key), an empty string should be returned. Timestamps for a given key are guaranteed to be strictly increasing.
Solution Approach
The solution uses a hash map (`store`) where keys are strings and values are lists of timestamp-value pairs. Each timestamp-value pair is stored as a tuple (Python), a custom `Pair` class (Java), or a pair (C++, C). The `set` operation simply appends a new timestamp-value pair to the list associated with the given key. The `get` operation uses binary search on the timestamp list for the given key to find the value associated with the largest timestamp less than or equal to the input timestamp.
Step-by-Step Algorithm
- Step 1: **Initialize the TimeMap:** Create a hash map (`store`) to store the key-value pairs. The key is a string, and the value is a list of (timestamp, value) tuples (or Pair objects).
- Step 2: **Set Operation (set(key, value, timestamp)):** Append the (timestamp, value) tuple to the list associated with the given key in the hash map.
- Step 3: **Get Operation (get(key, timestamp)):**
- Step 3a: Check if the key exists in the hash map. If not, return an empty string.
- Step 3b: Retrieve the list of (timestamp, value) tuples associated with the key.
- Step 3c: Perform binary search on the list of timestamps to find the largest timestamp less than or equal to the given timestamp.
- Step 3d: If a timestamp is found that is less than or equal to the input timestamp, store the corresponding value as the result.
- Step 3e: If no such timestamp is found (meaning the input timestamp is smaller than all timestamps in the list), return an empty string.
- Step 3f: Return the stored result value.
Key Insights
- Insight 1: Using a hash map (or dictionary) is efficient for storing the key-timestamp-value data because it provides fast average-case lookup by key.
- Insight 2: For each key, storing the timestamps and values as a sorted list enables the use of binary search to efficiently find the appropriate value for a given timestamp, fulfilling the requirement to return the value with the largest timestamp less than or equal to the input timestamp.
- Insight 3: Handling the edge case where no timestamp exists that's less than or equal to the target timestamp is crucial. The algorithm must return an empty string in this situation.
Complexity Analysis
Time Complexity: O(log n)
Space Complexity: O(n)
Topics
This problem involves: Hash Table, String, Binary Search, Design.
Companies
Asked at: Airbnb, Anduril, Axon, CARS24, Coinbase, Compass, Confluent, CrowdStrike, Databricks, Flexport, Gusto, Instacart, Lyft, Netflix, Nextdoor, Notion, OpenAI, Snowflake, VMware, Verkada, eBay.