Hash maps, prefix sums, and frequency counting
Arrays and hash maps are the bedrock of coding interviews. Nearly every problem uses one or both. Before diving into advanced data structures, you must be fluent in these fundamentals.
An array stores elements in contiguous memory, giving random access by index. The key complexity tradeoffs:
| Operation | Array | Dynamic Array | |-----------|-------|--------------| | Access by index | | | | Append | — | Amortized | | Insert at position | | | | Search (unsorted) | | | | Search (sorted) | | |
In Python, list is a dynamic array. In C++ and Java, use vector and ArrayList.
A hash map (dictionary) maps keys to values with average insert, delete, and lookup. This is the single most useful data structure in interviews.
When to use a hash map:
Example — Two Sum. Given an array and a target, find two elements that sum to the target.
def two_sum(nums, target):
seen = {} # value -> index
for i, num in enumerate(nums):
complement = target - num
if complement in seen:
return [seen[complement], i]
seen[num] = i
One pass, time, space. Without the hash map, you would need nested loops.
A prefix sum array lets you compute the sum of any subarray in after preprocessing:
prefix = [0] * (len(nums) + 1)
for i in range(len(nums)):
prefix[i + 1] = prefix[i] + nums[i]
# Sum of nums[l..r] inclusive:
subarray_sum = prefix[r + 1] - prefix[l]
Finance application. Prefix sums compute cumulative P&L instantly. "What was the total return between day and day ?" becomes a constant-time lookup.
Many problems reduce to counting. Python's collections.Counter is your best friend:
from collections import Counter
counts = Counter(nums)
# Most common k elements:
top_k = counts.most_common(k)
Pattern — Anagram detection. Two strings are anagrams if and only if they have the same character frequency counts. Compare counters in .
Given an array of stock tickers, group all anagram tickers together.
from collections import defaultdict
def group_anagrams(tickers):
groups = defaultdict(list)
for t in tickers:
key = tuple(sorted(t))
groups[key].append(t)
return list(groups.values())
Sorting each ticker takes where is the ticker length, and we do this for tickers: total.
Interview Tip: When you see "find duplicates," "count occurrences," or "group by property," immediately think hash map. State your data structure choice and its complexity before coding — interviewers want to hear your reasoning.