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 C#. 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; HashSet and Dictionary in C# (Add, TryGetValue, GetValueOrDefault); 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
- HashSet and Dictionary: Add, Contains, Remove, the indexer, TryGetValue, GetValueOrDefault
- Seen before? A set, and a set as a window of the last k numbers
- Look up the complement: Two Sum with a Dictionary
- Counting with a Dictionary, and with int[26]
- Grouping by a key, and set operations on two arrays
- The longest consecutive run: only start where a run starts
- Designing a set with a random pick: a Dictionary plus a List
- Five traps: KeyNotFoundException, Add on a repeated key, array keys, GetHashCode, adding while looping
Chapters
- 0:00Intro
- 0:22How a hash set works
- 1:35Four questions hashing answers
- 2:03What each structure costs
- 2:44HashSet and Dictionary in C#
- 4:11Five traps
- 5:18Nine hashing types
- 5:58Type 1 · Is there a duplicate?
- 11:31Type 2 · Duplicates close together
- 17:28Type 3 · Two numbers that add up
- 23:31Type 4 · Are they anagrams?
- 29:22Type 5 · Can the note be written?
- 35:20Type 6 · Group the anagrams
- 41:05Type 7 · What do two arrays share?
- 46:59Type 8 · Longest consecutive run
- 53:02Type 9 · A set with a random pick
- 59:59Spot the type
- 1:00:45Quiz: which type?
- 1:01:33Recap
Quiz: test yourself
The 5 questions from the end of the episode. Pick an answer to see why it's right.