Command Palette

Search for a command to run...

Module 4

Hashing

Trade a little memory for a lot of speed: remember what you've seen, count it, or group by a key, all in O(1) per lookup.

Beginner 4 lessons 6 problems ~45 min of lessons

Hash maps and hash sets are the most useful tools in coding interviews. Whenever a slow solution searches for something it has already passed ("have I seen x?", "where was the partner of x?"), a hash structure usually turns that search into a single O(1) lookup.

This module explains how they work inside, so you can predict their cost and avoid their traps, then drills the three main uses: complement lookup, counting, and grouping by a computed key.

Best after: Arrays, Strings

Part 1

Learn the ideas

Part 2

Solve the problems

In order of difficulty. Each one shows the pattern it teaches.

  1. The complement trick: for each number, look up target − number among the numbers already seen.

  2. Compare two strings by letter counts with a 26-slot array instead of sorting.

  3. Two passes: count everything first, then use the counts while scanning in the original order.

  4. Grouping by a computed key: equivalent items must produce the same key, different items different keys.

  5. Count first, then select: bucket sort by frequency gives O(n), beating a full sort.

  6. Use a set to find where runs start (x − 1 absent), then count each run once: O(n) without sorting.