Implement a Custom Data Store Class
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2019-08-01
Problem Statement
Design and implement a Custom Data Store class that supports storing, retrieving, updating, and deleting key-value pairs. Unlike a standard HashMap, this data store should be designed to handle extensibility, such as adding features like TTL (time-to-live), persistence, or versioning in the future.
✅ Functional Requirements
1.<span></span>Core Operations
2.<span></span>put(key, value) → Insert or update a key-value pair.
3.<span></span>get(key) → Retrieve the value for a key (if it exists).
4.<span></span>delete(key) → Remove a key-value pair.
5.<span></span>exists(key) → Check if a key is present.
6.<span></span>size() → Return the total number of entries.
7.<span></span>Additional Features
8.<span></span>Handle duplicate keys by overwriting values.
9.<span></span>Allow generic types for both keys and values.
10.<span></span>Support iteration over keys/values.
⚙️ Non-Functional Requirements
1.<span></span>Performance: Aim for O(1) for put/get/delete operations (using hashing or efficient indexing).
2.<span></span>Extensibility: The design should be modular so features like TTL expiry, disk persistence, or caching can be added later.
3.<span></span>Error Handling: Gracefully handle invalid operations (e.g., fetching a non-existent key).
4.<span></span>Thread Safety: (Bonus/Optional) Allow concurrent access with minimal locking.
🔐 Constraints
1.<span></span>The internal data structure should be implemented from scratch (no direct use of built-in HashMap/Dictionary).
2.<span></span>Must support at least 10⁶ key-value pairs efficiently.
3.<span></span>Keys must be unique.
🧱 Suggested Components
1.<span></span>Entry<K, V>
2.<span></span>Represents a key-value pair.
3.<span></span>CustomDataStore<K, V>
4.<span></span>Internal storage (e.g., array of buckets).
5.<span></span>Core methods: put, get, delete, exists, size.
6.<span></span>May internally use hashing + chaining / open addressing.
🧪 Example Walkthrough
<div><div><br /></div><div>Put("user1", "Alice") → DS = {"user1": "Alice"}</div><div>Put("user2", "Bob") → DS = {"user1": "Alice", "user2": "Bob"}</div><div>Get("user1") → "Alice"</div><div>Put("user1", "Carol") → DS = {"user1": "Carol", "user2": "Bob"}</div><div>Delete("user2") → DS = {"user1": "Carol"}</div><div>Size() → 1</div></div>👉 This LLD problem tests a candidate’s ability to:
1.<span></span>Implement core data structures (hashing, bucket management, collision resolution).
2.<span></span>Write clean, modular, and extensible OOP-based code.
3.<span></span>Consider scalability and performance trade-offs.