Counting Coding Problems: 65 Questions with Solutions

65 counting coding problems — 32 easy · 29 medium · 4 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 8-day plan.

  • Problems: 65
  • By difficulty: 32 easy · 29 medium · 4 hard
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
  • Cost: Free on every plan; sign in to run and submit

Problems answered by tallying: how many of each value, how many pairs satisfy a rule, how many ways to reach a total. A frequency array or map is usually the whole solution; the skill is choosing what to count and noticing when the count can be updated incrementally instead of recomputed.

How counting works, step by step

sl0o1o2p3tp0o1o2l3countl0o0p0
Valid anagram, with a letter-count table. Example: s = "loop", t = "pool"
  1. "loop" and "pool" both have 4 letters, so they are anagrams exactly when each letter appears equally often in both. Keep one counter per letter: s adds one, t takes one away, and every counter must end at 0.
  2. s[0] = 'l', so count[l] goes up to 1: one 'l' that t will have to match.
  3. s[1] = 'o', so count[o] goes up to 1.
  4. s[2] = 'o' again, so count[o] rises to 2: t will need 2 of them to match.
  5. s[3] = 'p', so count[p] goes up to 1.
  6. Now t takes letters back: t[0] = 'p', so count[p] drops to 0. A counter going below 0 would mean t has a letter s lacks, and the answer would be false at once.
  7. t[1] = 'o', so count[o] drops to 1: s still has 1 more 'o' than t so far.
  8. t[2] = 'o', so count[o] drops to 0: s and t have used the same number of 'o'.
  9. t[3] = 'l', so count[l] drops to 0: s and t have used the same number of 'l'.
  10. Every counter is back to 0, so "pool" uses exactly the letters of "loop": they are anagrams. Two passes over n letters and at most 26 counters: O(n) time, O(1) extra space.

Counting study plan

14 of the 65 Counting problems (4 easy, 7 medium and 3 hard) over 8 days, about 8 h 10 min in all — the pattern first, then easiest to hardest. After that, the other 51 in the full list below are practice at your own pace. Then move on to Math.

Day 1

Learn the pattern: read the essentials and step through the walkthrough above, then solve these 3.

Day 2

Medium problems: the same pattern with one twist each. Name the twist before you code.

Day 3

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 4

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 5

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 6

Hard problems: the pattern combined with a second idea. Give each a full attempt before reading the editorial.

Day 7

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Day 8

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Next topic: Math

Counting: the essentials

When to reach for it

"How many pairs", "most frequent", "can these letters build that word", "make every count equal", "the first value that appears once". If the answer depends only on how often each value occurs, not where, discard the positions and count. If it asks for pairs of equal values, arithmetic on the counts replaces the pair loop. The same tally keyed by anything other than small integers is a Hash Table.

The pattern

Build the tally in one pass, then reason about the counts. A value seen c times forms c × (c − 1) ÷ 2 equal pairs; counting as you go — adding the current count before incrementing it — gives the same total in a single pass. "Can A be built from B" is a check that every count in A is at most the matching count in B.

def num_identical_pairs(nums):
    seen, pairs = {}, 0
    for x in nums:
        pairs += seen.get(x, 0)     # x pairs with every earlier copy
        seen[x] = seen.get(x, 0) + 1
    return pairs

Cost

O(n) time. Space is O(k) for k distinct values, which is O(1) for a fixed alphabet such as the 26 lowercase letters.

Common mistakes

  • Counting ordered pairs when the question wants unordered ones, or the reverse — a factor of two either way.
  • Overflow: 10⁵ equal values form about 5 × 10⁹ pairs, beyond a 32-bit int.
  • Recounting from scratch for every query when the tally can be updated one element at a time.
  • Checking only the letters of one word when both words' counts matter.

Start with

All counting problems

Easy (32)

Medium (29)

Hard (4)

Companies that ask counting problems

Next topic: Math