Visualized DSA Strategies
Episode 2 Hashing: HashSet and HashMap
▶ This video is coming to YouTube soon.
The complete guide to hashing for coding interviews, in Python. First how a hash set really works: buckets, collisions and why lookups are O(1) on average; the four questions hashing answers; what each structure costs; sets and dicts in Python (ask with in, because add returns None; get with a default; Counter and defaultdict); and five traps. Then nine types, each worked as a small problem in full: examples, the brute force animated beside the running line of code, the complete code, every line explained, and the hashing solution the same way.
What you'll learn
- How a hash function, buckets and collisions make lookups O(1) on average
- Sets and dicts in Python: add, in, discard, get with a default, setdefault, Counter, defaultdict and items
- Seen before? A set, and a set as a window of the last k numbers
- Look up the complement: Two Sum with a dict
- Counting with a dict or a Counter, and with a list of 26 counts
- Grouping by a key with defaultdict(list), and set operations on two lists
- The longest consecutive run: only start where a run starts
- Designing a set with a random pick: a dict plus a list
- Five traps: is vs ==, KeyError on a missing key, lists as keys, __eq__ without __hash__, changing a dict while looping over it
Chapters
- 0:00Intro
- 0:22How a hash set works
- 1:34Four questions hashing answers
- 2:01What each structure costs
- 2:42Sets and dicts in Python
- 4:24Five traps
- 5:30Nine hashing types
- 6:10Type 1 · Is there a duplicate?
- 11:52Type 2 · Duplicates close together
- 17:53Type 3 · Two numbers that add up
- 23:36Type 4 · Are they anagrams?
- 29:06Type 5 · Can the note be written?
- 35:11Type 6 · Group the anagrams
- 40:50Type 7 · What do two arrays share?
- 46:51Type 8 · Longest consecutive run
- 53:03Type 9 · A set with a random pick
- 59:44Spot the type
- 1:00:31Quiz: which type?
- 1:01:18Recap
Quiz: test yourself
The 5 questions from the end of the episode. Pick an answer to see why it's right.