Hash Tables from Scratch Checkpoint
A review of this module. Skip it if you feel confident; the next module does not need it.
Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
This checkpoint mixes slots, collisions, resizing and designs. Work each answer out by hand, then run the code to check it.
Slots, collisions and probing
Given a table size and keys, you should be able to say each key's slot, which keys collide, and where linear probing places them. Work it out on paper first, then check with code.
slots = [None] * 5for key in [12, 7, 22, 3]: i = key % 5 while slots[i] is not None: i = (i + 1) % 5 slots[i] = keyprint(slots)Example 1 · Anagram groups by counting
words = ["tea", "eat", "tan", "ate", "nat", "bat"]groups = {}for word in words: groups.setdefault("".join(sorted(word)), []).append(word)print(len(groups), sorted(map(len, groups.values())))3 [1, 2, 3]
What this code does
- Words with the same sorted letters share a key, so the dictionary groups them in one pass.
- The keys are strings, so a hash is used, but only the grouping is printed, which does not depend on the hash value.
Keys 12, 7 and 22 all start at slot 2. They take slots 2, 3 and 4. Key 3 wants slot 3, finds it taken and moves on to slot 4, then wraps to slot 0.
Load factor and resizing
A table grows when it is too full. Know the load factor, the doubling rule and why a resize rehashes every key. Count how many times a table doubles for a given number of inserts.
capacity, resizes = 4, 0for count in range(1, 41): if count > 0.75 * capacity: capacity *= 2 resizes += 1print(capacity, resizes)Example 2 · First repeated order id
ids = [41, 17, 8, 17, 41, 99]seen = set()for order in ids: if order in seen: print(order) break seen.add(order)17
What this code does
- A set answers "seen before?" in constant time on average.
- The scan stops at the first repeat, 17. A nested loop would take O(n squared) time.
The table doubles from 4 to 8 at the 4th key, to 16 at the 7th, to 32 at the 13th and to 64 at the 25th. A table with 40 keys ends at 64 slots after 4 resizes.
Hash map in a design
When a problem needs constant-time lookup plus order, a random pick or a count, pair a dictionary with a second structure. Say the time and the space out loud: here O(1) per operation and O(n) space.
from collections import OrderedDictrecent = OrderedDict()for key in [1, 2, 1, 3]: recent[key] = True recent.move_to_end(key)print(list(recent))Example 3 · Resize moves keys
keys = [5, 9, 14, 3]print([k % 4 for k in keys])print([k % 8 for k in keys])[1, 1, 2, 3] [5, 1, 6, 3]
What this code does
- With 4 slots keys 5 and 9 share slot 1.
- After doubling to 8 slots all four keys have their own slot, so the remainders changed and each key had to be placed again.
Using key 1 again moves it to the end, so the least recently used key is always first in the dictionary.
Module quiz
Questions from the whole module. Your first answer is your score.
Problems
Graded easy to hard, using everything from this module.
Try each one yourself first. Use the hint if you get stuck. Mark it as done when it works.
Exercise 1· Easy
Print the slots (key % 7) of keys [15, 22, 29, 8] as a list.
[1, 1, 1, 1]
Exercise 2· Easy
Print the load factor of a table with 24 keys in 32 slots.
0.75
Exercise 3· Easy
Print the number of different words in ["pen", "ink", "pen", "pad", "ink"] using a set.
3
Exercise 4· Medium
Insert [10, 18, 26, 3] into 8 slots with linear probing and print the table.
[None, None, 10, 18, 26, 3, None, None]
Exercise 5· Medium
Print how many resizes happen for 100 inserts when the capacity starts at 4 and doubles if count is more than 0.75 of the capacity.
6
Exercise 6· Medium
Count the pairs of equal numbers (i < j) in [1, 3, 1, 3, 3, 2] with a dictionary of counts.
4
Exercise 7· Hard
A cache of capacity 2 gets the keys [1, 2, 1, 3, 2]. Using LRU eviction, print the keys left in the cache from oldest to newest.
[3, 2]
Exercise 8· Hard
Print the length of the longest stretch of distinct numbers in [4, 2, 4, 5, 6, 2, 7] (a window with no repeats), using a dictionary of last positions.
5
Interview questions
Questions interviewers ask on this topic.
Finish up
Mark complete & continue
Next: Must-do problems: Hash Tables from Scratch