Pattern

Counting problems

Count how many ways, or how many occurrences, without enumerating every possibility one at a time.

What it is

Counting questions ask "how many" rather than "which one" or "what is the value of" — how many combinations reach a target, how many times a value repeats, how many subsequences share a property. The trap is that "how many" sounds like it invites brute-force enumeration, but the intended solutions almost always avoid ever listing the things being counted: a running tally derived from a vote/majority argument, a dynamic-programming table where each cell holds a count rather than a best value, or a count carried alongside a length in a DP state so you get "how many" and "how long" from the same pass.

When it applies

Tells in the problem

The classic mistake

Iterating choices in the wrong order (amounts on the outside, coins on the inside) so that the same combination gets counted once per ordering of its parts, inflating a "combinations" answer into what is really a "permutations" answer, or vice versa when the question wanted permutations and got combinations. This happens because the recurrence for "ways to reach X" looks almost identical whether order matters or not — the only difference is which loop is on the outside — so it's easy to write a correct-looking DP that answers a subtly different question than the one asked. Before coding, restate in your own words whether [2,3] and [3,2] should count once or twice, and let that answer decide the loop order, rather than choosing the loop order first and hoping it matches.

Questions to start with

All 3 counting problems 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.