Pattern

Hash sets

Use O(1) membership testing to skip a nested scan, whether for duplicates, ranges, or valid splits.

What it is

A hash set gives O(1) average-case membership testing, and a surprising number of problems that look like they need sorting or nested loops actually just need the question "have I seen this value (or this suffix, or this boundary) before?" answered quickly. Once you have a set of everything relevant, checking a candidate against it replaces an O(n) inner scan with O(1), turning an O(n²) brute force into O(n) overall. The set itself is usually trivial to build — the skill is recognizing which values belong in it and what question you're really asking against it.

When it applies

Tells in the problem

The classic mistake

Rebuilding or rescanning the set (or worse, the whole array) inside what should be an O(1) check, silently turning the intended O(n) solution back into O(n²) — in the consecutive-sequence problem this looks like starting a "count how long this run is" scan from every single element instead of only from elements that are confirmed run-starts (a value whose predecessor isn't in the set). The fix is to make the entry condition into the loop explicit and cheap to check (one set lookup) before doing any real work inside it, so each element is the start of a scan at most once across the whole run of the algorithm.

Questions to start with

All 3 hash sets questions

Problem statement, examples and hints are free on every page; the worked approach, solution and tests are in the company guides.

Free starter pack

13 must-tier questions with full solutions

Sign in with an email — no purchase — and read them in the full reader.

No password, no spam. One sign-in email; you can read the pack in the full reader.