Visualized DSA Strategies

Episode 2 Hashing: HashSet and HashMap

1 h 4 min · PHP · 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 PHP. First how a hash table really works: buckets, collisions and why lookups are O(1) on average; the four questions hashing answers; what each structure costs; the PHP array as a set and a map (isset, unset, ??, array_count_values, array_flip); 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
  • The PHP array as a set and a map: isset, unset, the ?? fallback, appending to a key, array_count_values, array_flip
  • Seen before? Values as keys, and a set as a window of the last k numbers
  • Look up the complement: Two Sum with an array used as a map
  • Counting with an array of counts, and with 26 slots from ord
  • 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 map plus a list
  • Five traps: in_array scans, missing keys, arrays as keys, numeric string keys, isset and null

Chapters

  1. 0:00Intro
  2. 0:25How a hash set works
  3. 1:39Four questions hashing answers
  4. 2:11What each structure costs
  5. 2:56The PHP array as a set and a map
  6. 4:28Five traps
  7. 5:29Nine hashing types
  8. 6:09Type 1 · Is there a duplicate?
  9. 12:19Type 2 · Duplicates close together
  10. 18:18Type 3 · Two numbers that add up
  11. 24:09Type 4 · Are they anagrams?
  12. 30:10Type 5 · Can the note be written?
  13. 36:34Type 6 · Group the anagrams
  14. 42:25Type 7 · What do two arrays share?
  15. 48:35Type 8 · Longest consecutive run
  16. 55:03Type 9 · A set with a random pick
  17. 1:02:10Spot the type
  18. 1:02:58Quiz: which type?
  19. 1:03:45Recap

Quiz: test yourself

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