Visualized DSA Strategies

Episode 2 Hashing: HashSet and HashMap

1 h 3 min · Go · Quiz

Title card of Episode 2: Hashing: HashSet and HashMap

▶ This video is coming to YouTube soon.

The complete guide to hashing for coding interviews, in Go. 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; maps and sets in Go (map[int]bool as the set, the zero value of a missing key, v, ok := m[k], append to group); 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
  • Go's map as a set and a counter: the zero value, v, ok := m[k], delete, len and range
  • Seen before? A set, and a set as a window of the last k numbers
  • Look up the complement: Two Sum with a map
  • Counting with a map, and with a [26]int array
  • Grouping by a key with append, and set operations on two slices
  • The longest consecutive run: only start where a run starts
  • Designing a set with a random pick: a map plus a slice
  • Five traps: the silent zero value, nil maps, slice keys, random map order, append without storing back

Chapters

  1. 0:00Intro
  2. 0:21How a hash set works
  3. 1:32Four questions hashing answers
  4. 2:02What each structure costs
  5. 2:43Maps and sets in Go
  6. 4:11Five traps
  7. 5:10Nine hashing types
  8. 5:51Type 1 · Is there a duplicate?
  9. 11:45Type 2 · Duplicates close together
  10. 17:47Type 3 · Two numbers that add up
  11. 23:38Type 4 · Are they anagrams?
  12. 29:21Type 5 · Can the note be written?
  13. 35:28Type 6 · Group the anagrams
  14. 41:14Type 7 · What do two arrays share?
  15. 47:12Type 8 · Longest consecutive run
  16. 53:38Type 9 · A set with a random pick
  17. 1:00:29Spot the type
  18. 1:01:16Quiz: which type?
  19. 1:02:04Recap

Quiz: test yourself

The 5 questions from the end of the episode. Pick an answer to see why it's right.