Top 4 Hash map Interview Questions
Browse 4 real-world technical and behavioral interview questions about Hash map. Review scenarios, edge cases, and architectural best practices.
Count the subarrays whose elements sum to k, where the values may be negative.
Subarray Sum Equals K is solved with prefix sums and a hash map: for each running sum, count earlier prefixes equal to sum-k. This is O(n) time and works with negative numbers, unlike a sliding window. Use this hashing answer to show the decision, trade-off, and evidence rather than a memorised definition.
Group a list of words so that words which are anagrams of each other end up together. What is your key, and what does it cost?
Every anagram class needs one canonical key: either the word's letters sorted, or a vector of letter counts. The counting key is cheaper but only if the counts are separated when they are turned into a string, because concatenating them lets a count of eleven and a count of one swap places unnoticed.
Insert, delete and get-a-uniformly-random-member all have to be O(1). What breaks when you delete from the middle?
Uniform sampling needs a gapless array you can index, so a deletion cannot leave a hole and cannot shift the tail. You overwrite the hole with the last element and shrink by one, which costs a fixed amount of work but silently moves another member, so the map from value to index must be corrected for the element that moved and not only for the one removed.
A cache must evict the least recently used entry and every operation has to be O(1). Which two structures do you need, and why is one of them not enough?
A hash map from key to a doubly linked list node, over one recency-ordered list. The map answers find-by-key, the list answers find-the-oldest, and neither answers the other in constant time. The detail that decides the answer is that the map stores the node rather than the value, which is what makes unlinking on a hit a fixed number of pointer writes.