Challenge your understanding of hashing techniques, hash maps, and their role in solving algorithmic problems such as Two Sum and Subarray Sum. Each question explores critical concepts regarding hash functions, collision resolution, and real-world applications.
Selecting Hash Functions
Given a set of integer keys where values are primarily multiples of 10, which property is MOST important for a good hash function to minimize clustering in a hash map of size 10?
- Has a time complexity greater than O(1)
- Ensures uniform distribution even with regular patterns in input data
- Is the same as the identity function
- Collapses all even numbers to the same bucket
- Maps every key to the same index
Collision Handling Strategies
Which hash collision resolution technique can lead to primary clustering, where long runs of occupied slots promote longer probe sequences in a hash table?
- Quadratic probing
- Double hashing
- Linear probing
- Chaining
- Separate hashing
HashMap Applications: Two Sum
In the classic Two Sum problem, why is a hash map preferred over a sorted array and binary search for achieving optimal time complexity?
- Hash maps avoid the need to store indices
- Hash map offers average O(1) lookup and insertion
- Hash maps require all keys to be unique, which is always true for Two Sum
- Binary search allows O(1) search on unsorted data
- Sorted array provides the fastest insertion
Designing Custom Hash Functions
If you are designing a hash function for strings where many keys share the same suffix, which approach would most REDUCE the risk of collisions?
- Weigh the prefix more heavily than the suffix in the computation
- Use only the ASCII value of the last character
- Map strings to their length only
- Ignore the middle character of each string
- Return a constant hash value for all strings
Subarray Sum Problem with Hash Maps
How does a hash map facilitate a more efficient solution to finding a contiguous subarray that sums to a target value in an array of integers?
- By maintaining the sorted order of all window subarrays
- By grouping numbers of similar value into buckets
- By always storing every possible subarray in advance
- By avoiding duplicate values in all subarrays
- By storing prefix sums and their earliest indices for constant-time lookups
Hash Map Load Factor Impact
What is the effect of increasing the load factor in an open-addressed hash table where the table is NOT resized?
- Collisions will no longer occur
- Average lookup and insertion times increase significantly
- Keys become permanently immutable
- Memory usage for the table decreases
- Insertion time stays at O(1) regardless of occupancy
Chaining vs. Open Addressing
When implementing hash map collision handling, which statement accurately describes a difference between chaining and open addressing?
- Open addressing reduces memory overhead compared to chaining in all cases
- Chaining cannot handle duplicate keys but open addressing can
- Chaining never uses pointers; open addressing always uses them
- Chaining uses linked lists to store entries; open addressing stores all entries within the array
- In open addressing, hash function is never used
Hash Map Key Design Pitfalls
When using user-defined objects as keys in a hash map, what common pitfall can lead to entries becoming 'invisible' or unreachable from the map?
- Having fields in the key set to null values
- Reusing the hash map in multiple threads
- Assigning keys sequential integer values
- Modifying the key's fields affecting its hash code after insertion
- Using objects with a toString method
Hash Maps and Duplicate Values
Which statement best explains how a hash map deals with duplicate values but unique keys?
- It allows multiple keys to map to the same value without issue
- Duplicate values throw a runtime exception
- It sorts the values and discards identical ones
- It does not permit any duplicate values at all
- Each new value replaces the map entirely
Selecting Hash Map Size
Why is it often recommended to use a prime number as the hash table size when implementing a modular hash function?
- So that the modulus operation is faster
- It is required for storing numeric keys
- To help achieve better key distribution and reduce clustering
- Because prime sizes conserve more memory
- Prime table sizes eliminate the need for collision handling