Note Time O(n), Space O(k) where k is unique elements. Python's Counter is a powerful shortcut. In interviews, building the map manually shows understanding. Tie-breaking: clarify with interviewer what to return if multiple elements share max frequency.
For each element, check if(target - element) exists in the map.Single pass: check then insert.
Example
// JavaScriptfunctiontwoSum(nums, target){const map =newMap();for(let i =0; i < nums.length; i++){const complement = target - nums[i];if(map.has(complement)){return[map.get(complement), i];}
map.set(nums[i], i);}return[];}
# Python
def two_sum(nums, target):
seen ={}for i, num inenumerate(nums):
complement = target - num
if complement in seen:return[seen[complement], i]
seen[num]= i
return[]
Output
two_sum([2, 7, 11, 15], 9) → [0, 1]
Note Time O(n), Space O(n). The classic interview opener. Single-pass approach works because we only need one pair. Edge cases: duplicate values, negative numbers, target is double of an element. For three-sum, sort + two pointers is preferred over triple hash map lookups.
two sumhash map paircomplement searchtarget sum
Group Anagrams
Syntax
Use sorted string(or character frequency tuple)as hash map key.All anagrams produce the same key.
Example
// JavaScriptfunctiongroupAnagrams(words){const groups =newMap();for(const word of words){const key = word.split('').sort().join('');if(!groups.has(key)) groups.set(key,[]);
groups.get(key).push(word);}returnArray.from(groups.values());}
# Python
def group_anagrams(words):
groups ={}for word in words:
key =''.join(sorted(word))
groups.setdefault(key,[]).append(word)returnlist(groups.values())
Note Sorting key: O(n * k log k) where k is max word length. Frequency key alternative: O(n * k) but more code. Interviewers may ask for the optimal approach - mention the frequency-tuple key to avoid k log k sorting per word.
group anagramsanagram groupinghash map groupingsorted key
Subarray Sum Equals K
Syntax
Use prefix sum + hash map.At each index, check if(currentPrefix - k) was seen before.Store prefix sum frequencies in the map.
Example
// JavaScriptfunctionsubarraySum(nums, k){const prefixCount =newMap([[0,1]]);let prefix =0, count =0;for(const num of nums){
prefix += num;if(prefixCount.has(prefix - k)){
count += prefixCount.get(prefix - k);}
prefixCount.set(prefix,(prefixCount.get(prefix)||0)+1);}return count;}
# Python
def subarray_sum(nums, k):
prefix_count ={0:1}
prefix =0
count =0for num in nums:
prefix += num
count += prefix_count.get(prefix - k,0)
prefix_count[prefix]= prefix_count.get(prefix,0)+1return count
Note Time O(n), Space O(n). Initialize map with {0: 1} to handle subarrays starting at index 0. This pattern is reusable: subarray sum divisible by k, subarray with equal 0s and 1s. One of the most asked medium-level problems.
subarray sumprefix sum hash mapsubarray sum equals kcontiguous sum
Intersection of Collections
Syntax
Convert one collection to a set, iterate the other and check membership.For sorted arrays: use two pointers instead.
Example
// JavaScriptfunctionintersection(arr1, arr2){const set1 =newSet(arr1);const result =[];const seen =newSet();for(const val of arr2){if(set1.has(val)&&!seen.has(val)){
result.push(val);
seen.add(val);}}return result;}
# Python
def intersection(arr1, arr2):returnlist(set(arr1)&set(arr2))
# With duplicates preserved:
def intersect_with_dupes(arr1, arr2):from collections importCounter
c1, c2 =Counter(arr1),Counter(arr2)returnlist((c1 & c2).elements())
Note Set approach: O(n + m) time, O(min(n,m)) space. Sorted two-pointer approach: O(n log n + m log m) time, O(1) extra space. Ask the interviewer: unique results or preserve duplicates? Are inputs sorted?
Note Both get and put must be O(1). In a real interview, they may want the DLL implementation from scratch - practice building a Node class with prev/next pointers. This is one of the most commonly asked design questions at top companies.