Design Key-Value Store with Collision Support
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-06-25
Problem Statement
Designing a key-value store that supports storing and retrieving key-value pairs efficiently. The system should handle collisions gracefully and allow basic operations like put, get, and delete. Focus is on object-oriented design, data structures, and modularity.
✅ Functional Requirements
1.Basic Operations
•put(key, value) → store a key-value pair.
•get(key) → retrieve the value for a given key.
•delete(key) → remove a key-value pair.
2.Collision Handling
•Handle collisions using chaining (linked lists) or open addressing (linear probing, quadratic probing).
•Ensure O(1) average time complexity for insert, lookup, and delete.
3.Dynamic Resizing
•Automatically resize the underlying storage (rehashing) when load factor exceeds threshold.
4.Optional Features
•Support for TTL (time-to-live) for keys.
•Support for persistent storage to save key-value pairs to disk.
🔐 Constraints & Assumptions
•Focus is on LLD, class design, and data structure implementation, not on distributed KV stores.
•Keys are strings, values can be strings or generic objects.
•Single-threaded environment (thread-safety optional).
🧱 Key Components & Classes
1.Entry Class
1
2class Entry {
3public:
4 string key;
5 string value;
6 Entry* next; // for chaining in case of collision
7 Entry(string k, string v): key(k), value(v), next(nullptr) {}
8};1.HashTable Class
1
2class HashTable {
3private:
4 vector<Entry*> buckets; // array of pointers to linked lists
5 int capacity;
6 int size;
7 float loadFactorThreshold;
8
9 int hashFunction(string key);
10 void rehash();
11public:
12 HashTable(int initialCapacity = 16);
13 void put(string key, string value);
14 string get(string key);
15 void remove(string key);
16};1.Collision Handling
•Using chaining with linked lists:
•Each bucket in the array contains a linked list of entries.
•On collision, new entry is appended to the list.
•Alternative: open addressing with probing can be implemented as another design variant.
🔄 Example Workflow
1.User calls put("user1", "Alice") → hash key → compute bucket index → insert into linked list.
2.Another key "u1ser" collides → append to same linked list in that bucket.
3.get("user1") → hash key → traverse linked list in the bucket → return value "Alice".
4.remove("user1") → hash key → traverse linked list → remove node.
5.If load factor exceeds threshold → rehash() to increase capacity and redistribute entries.
🧪 Expected Deliverables
•Class diagram showing Entry and HashTable relationships.
•Implementation of put, get, remove, collision handling, and rehashing.
•Test cases:
•Insert multiple keys with collisions
•Retrieve values correctly
•Delete keys and check integrity
•Trigger rehashing by exceeding load factor
💡 Discussion Points
•Collision Strategy: chaining vs open addressing.
•Hash Function: efficient, uniform distribution.
•Resizing: rehashing cost and amortized complexity.
•Optional: TTL, persistence, and concurrency.