Implement a hash table with put/get
Problem Description
Implement a simplified hash table (hash map) data structure from scratch.
Required API
Design a class HashTable with the following methods:
HashTable(int numBuckets): A constructor to initialize the internal storage with a fixed number of buckets.void put(K key, V value): Inserts a key/value pair. If the key already exists, its value should be overwritten.V get(K key): Returns the value associated with the givenkey. If the key is not present, return a sentinel value (e.g.,nullor-1).
Note: The delete or remove operation is explicitly not required for this implementation.
Assumptions and Constraints
- Keys are guaranteed to be non-null.
- Your implementation must correctly handle hash collisions.
- The target average time complexity for both
putandgetoperations should be O(1) (amortized/expected). - You are prohibited from using any built-in hash map or dictionary data structures provided by the language.
Conceptual Follow-up Discussion Points
- What characteristics define a "good" hash function, and how does the distribution of keys impact the overall performance of the hash table?
- Discuss how the initial choice of
numBucketsand the resulting load factor influence collision rates and the runtime performance of operations. - Compare and contrast common strategies for resolving hash collisions, specifically:
- Separate chaining (using linked lists or similar structures in buckets)
- Open addressing (e.g., linear probing, quadratic probing, double hashing)
- If an open addressing strategy is chosen, describe the mechanism of probing (or "hopping") and identify potential pitfalls such as clustering and the need for tombstones.
AI interview prep
Core dilemma
Balancing the theoretical O(1) average time complexity with practical considerations like memory usage, collision handling efficiency, and the impact of a fixed-size underlying storage.
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 hash table with put/get
Problem Description
Implement a simplified hash table (hash map) data structure from scratch.
Required API
Design a class HashTable with the following methods:
HashTable(int numBuckets): A constructor to initialize the internal storage with a fixed number of buckets.void put(K key, V value): Inserts a key/value pair. If the key already exists, its value should be overwritten.V get(K key): Returns the value associated with the givenkey. If the key is not present, return a sentinel value (e.g.,nullor-1).
Note: The delete or remove operation is explicitly not required for this implementation.
Assumptions and Constraints
- Keys are guaranteed to be non-null.
- Your implementation must correctly handle hash collisions.
- The target average time complexity for both
putandgetoperations should be O(1) (amortized/expected). - You are prohibited from using any built-in hash map or dictionary data structures provided by the language.
Conceptual Follow-up Discussion Points
- What characteristics define a "good" hash function, and how does the distribution of keys impact the overall performance of the hash table?
- Discuss how the initial choice of
numBucketsand the resulting load factor influence collision rates and the runtime performance of operations. - Compare and contrast common strategies for resolving hash collisions, specifically:
- Separate chaining (using linked lists or similar structures in buckets)
- Open addressing (e.g., linear probing, quadratic probing, double hashing)
- If an open addressing strategy is chosen, describe the mechanism of probing (or "hopping") and identify potential pitfalls such as clustering and the need for tombstones.