Hash usage
easy 16 minHash maps and hash sets find a key by computing where it lives instead of searching for it, so insert, lookup and delete cost O(1) on average.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
What is the average time complexity of a hash table lookup?
What happens when two different keys hash to the same index?
What is a common load factor threshold for resizing a hash table?
Which of these is NOT a collision resolution strategy?
Running two_sum([3, 2, 4], 6), where each number is stored in the dict BEFORE its complement is checked, returns what?
freq = {} and then freq['a'] += 1. What happens?
Which of these can be used as a dict key?
subarray_sum([1, 1, 1], 2) with the prefix-sum map returns what?
You must report the smallest key at least x, many times, while keys are inserted and deleted. Which structure fits best?
d = {'red': 1, 'blue': 2}. What does 2 in d return?
Pick an answer to see why.
Not quite. The key's hash gives its slot directly, and the load factor is kept bounded, so only a constant number of keys are compared on average. The worst case is O(n).
Not quite. That is a collision. The table keeps both keys, for example in a chain at that slot or by probing to another slot, and tells them apart by comparing the keys themselves.
Not quite. Java's HashMap resizes at 0.75 by default; CPython's dict resizes at about 2/3. Both keep slots sparse enough for O(1) average operations without wasting much memory.
Not quite. Chaining, linear probing and double hashing all decide where a colliding key goes. Binary search is a search algorithm on sorted data.
Not quite. At index 0 the code stores 3 → 0, then looks for 6 − 3 = 3 and finds the 3 it just stored. Checking first and storing second gives the correct [1, 2].
Not quite. += reads freq['a'] first, and the key does not exist. Use freq.get('a', 0) + 1, a defaultdict(int), or a Counter.
Not quite. A tuple of immutable values is hashable. A list and a set are mutable, and a tuple that contains a list is unhashable because of the list inside it.
Not quite. Prefixes are 1, 2, 3. At prefix 2 the map has seen 0 once (subarray [1, 1] at indexes 0-1); at prefix 3 it has seen 1 once (indexes 1-2). Two subarrays.
Not quite. That is a ceiling query, which needs key order. A hash structure keeps none, so it would scan all keys in O(n); a balanced BST answers in O(log n).
Not quite. in tests keys, not values. 2 is a value, so the result is False. 2 in d.values() would be True, after an O(n) scan.
Flashcards
Say your answer, then reveal it.
Card 1 of 11
Average and worst-case cost of dict insert, lookup and delete?
O(1) average, O(n) worst case when many keys collide into the same slot.
Say your answer, then reveal it.
Why is a hash lookup O(1) on average?
The location is computed from the key's hash instead of searched for, and the table resizes to keep the load factor n/m bounded, so each slot holds only a few keys.
Say your answer, then reveal it.
What makes a valid dict key or set member?
It must be hashable: immutable types such as int, str, tuple of hashables, frozenset. Lists, dicts and sets are not.
Say your answer, then reveal it.
dict, set, Counter, defaultdict: one line each?
dict maps key to value; set holds unique keys; Counter is a dict of counts with most_common; defaultdict fills in a default (list, int, set) for missing keys.
Say your answer, then reveal it.
Two Sum with a dict: what do you store, and in what order?
Store value to index. For each number, first check whether target minus it is already a key, then store it, so a number never pairs with itself. O(n) time, O(n) space.
Say your answer, then reveal it.
Canonical keys for grouping anagrams?
The sorted letters as a string (O(L log L) per word) or a tuple of 26 letter counts (O(L) per word).
Say your answer, then reveal it.
Prefix sum plus map: what is looked up?
prefix minus k. The dict counts earlier prefix sums, seeded with {0: 1}; each match is a subarray ending here with sum k.
Say your answer, then reveal it.
d[k] vs d.get(k) on a missing key?
d[k] raises KeyError; d.get(k) returns None, or the default you pass as the second argument.
Say your answer, then reveal it.
What does x in d check?
Keys only, in O(1) average. Checking values needs x in d.values(), which is an O(n) scan.
Say your answer, then reveal it.
Does a Python dict keep order?
Insertion order, guaranteed since 3.7. Never sorted by key. A set promises no order; Java's HashMap promises none either.
Say your answer, then reveal it.
How do you remove duplicates but keep the original order?
list(dict.fromkeys(items)): a dict keeps the first occurrence of each key in insertion order. list(set(items)) loses the order.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.