Skip to content
Preptima

Hashing & Maps interview questions

Trading memory for time with hash maps and sets, plus the collision and ordering caveats interviewers probe.

4 questions in Data Structures & Algorithms.

medium4

mediumCodingConcept

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.

4 minmid, senior
mediumCodingConcept

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.

5 minmid, senior, staff