Enumeration Coding Problems: 55 Questions with Solutions
55 enumeration coding problems — 17 easy · 29 medium · 9 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 8-day plan.
- Problems: 55
- By difficulty: 17 easy · 29 medium · 9 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
Sometimes the right answer is to try every candidate — every pair, every substring, every subset — because the bounds are small enough and a cleverer idea would cost more than it saves. The problems here practise generating the candidates without repetition and stopping the enumeration as soon as the bounds allow.
How enumeration works, step by step
nums = [2, 7, 4, 5, 3], k = 3- Count the pairs whose sum is divisible by 3 by trying every pair i < j: i fixes the first number and j runs over everything after it. With 5 numbers that is 5 × 4 / 2 = 10 pairs, one cell each in the grid.
- i = 0, j = 1: 2 + 7 = 9, which is divisible by 3, so this pair counts and count = 1.
- i = 0, j = 2: 2 + 4 = 6, which is divisible by 3, so this pair counts and count = 2.
- i = 0, j = 3: 2 + 5 = 7 leaves remainder 1 when divided by 3, so it does not count.
- i = 0, j = 4: 2 + 3 = 5 leaves remainder 2 when divided by 3, so it does not count.
- i moves on to 1 and j restarts just after it. i = 1, j = 2: 7 + 4 = 11 leaves remainder 2 when divided by 3, so it does not count.
- i = 1, j = 3: 7 + 5 = 12, which is divisible by 3, so this pair counts and count = 3.
- i = 1, j = 4: 7 + 3 = 10 leaves remainder 1 when divided by 3, so it does not count.
- i moves on to 2 and j restarts just after it. i = 2, j = 3: 4 + 5 = 9, which is divisible by 3, so this pair counts and count = 4.
- i = 2, j = 4: 4 + 3 = 7 leaves remainder 1 when divided by 3, so it does not count.
- i moves on to 3 and j restarts just after it. i = 3, j = 4: 5 + 3 = 8 leaves remainder 2 when divided by 3, so it does not count.
- All 10 pairs are checked, and 4 have a sum divisible by 3: (2, 7), (2, 4), (7, 5) and (4, 5). Two nested loops make it O(n²) time — fine for small inputs, and the baseline any faster idea must beat.
Enumeration study plan
14 of the 55 Enumeration 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 41 in the full list below are practice at your own pace. Then move on to Intervals.
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.
- Count Symmetric Integers Easy
- Reordered Power of 2 Medium
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.
- Additive Number Medium
- Powerful Integers Medium
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.
- Naming a Company Hard
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.
Enumeration: the essentials
When to reach for it
The limits are small enough to count the work in advance: n ≤ 100 allows every triple (about 1.6 × 10⁵ for n = 100), n ≤ 20 every subset (about a million), and values up to 10⁴ allow trying every candidate answer. "Count the triplets that satisfy…", "how many integers in [low, high] are symmetric" and "can these digits be rearranged into a power of two" are meant to be enumerated — over the right set.
The pattern
Estimate first: candidates × cost of checking each should stay around 10⁷–10⁸ simple steps. Then choose what to enumerate, usually the smaller side. Reordered Power of 2 does not try the orderings of n's digits (up to 10! ≈ 3.6 million); it compares digit counts with the 31 powers of two below 2³¹. Generate each candidate once: start j at i + 1, not at 0.
from collections import Counter
def reordered_power_of_2(n):
digits = Counter(str(n))
return any(Counter(str(1 << k)) == digits for k in range(31))
Cost
Exactly the number of candidates times the cost of each check: O(n³) for triples, O(2ⁿ × n) for subsets, O(range × digits) for a numeric range. An early exit improves the constant; choosing what to enumerate changes the bound.
Common mistakes
- Counting one combination several times by starting every loop at index 0.
- Enumerating the larger of two equivalent sets, such as the digit orderings rather than the targets.
- Skipping the estimate: with n = 10⁴, an O(n³) loop is 10¹² steps.
- An off-by-one at either end of an inclusive range
[low, high].
Start with
- Count Good Triplets: every triple, checked directly.
- Count Square Sum Triples: enumerating two values, deriving the third.
- Reordered Power of 2: enumerating the targets instead.
All enumeration problems
Easy (17)
- Latest Time You Can Obtain After Replacing Characters String
- Number of Valid Clock Times String
- Greatest English Letter in Upper and Lower Case Hash Table, String
- Remove Digit From Number to Maximize Result String, Greedy
- Finding 3-Digit Even Numbers Array, Hash Table, Sorting
- Count Special Quadruplets Array, Hash Table
- Detect Pattern of Length M Repeated K or More Times Array
- Form Smallest Number From Two Digit Arrays Array, Hash Table
- Count the Number of Incremovable Subarrays I Array, Two Pointers
- Maximum Height of a Triangle Array, Greedy, Simulation
- Binary Watch Bit Manipulation, Backtracking
- Find the Peaks Array
- Number of Common Factors Math, Number Theory
- Count Symmetric Integers Math
- Count Square Sum Triples Math
- Number of Arithmetic Triplets Array, Hash Table, Two Pointers
- Count Good Triplets Array
Medium (29)
- Collecting Chocolates Array
- Count Special Subsequences Array, Hash Table, Math
- Identify the Largest Outlier in an Array Array, Hash Table, Counting
- Count Almost Equal Pairs I Array, Hash Table, Sorting
- Find the Maximum Number of Elements in Subset Array, Hash Table
- Maximum Square Area by Removing Fences From a Field Array, Hash Table
- Number of Black Blocks Array, Hash Table
- Maximize Active Section with Trade I String
- Find the Lexicographically Largest String From the Box I Two Pointers, String
- Count Beautiful Substrings I Hash Table, Math, String
- Largest Time for Given Digits Array, String
- Lexicographically Smallest String After Applying Operations String, Depth-First Search, Breadth-First Search
- Ambiguous Coordinates String, Backtracking
- Splitting a String Into Descending Consecutive Values String, Backtracking
- Image Overlap Array, Matrix
- Maximum Strength of a Group Array, Greedy, Sorting
- Minimum Cost to Set Cooking Time Math
- K Divisible Elements Subarrays Array, Hash Table, Trie
- Maximum Possible Number by Binary Concatenation Array, Bit Manipulation
- Maximum Rows Covered by Columns Array, Bit Manipulation, Matrix
- Prime Pairs With Target Sum Array, Math, Number Theory
- Minimum Operations to Make the Integer Zero Bit Manipulation, Brainteaser
- Sum of Number and Its Reverse Math
- Count Lattice Points Inside a Circle Array, Hash Table, Math
- Powerful Integers Hash Table, Math
- Additive Number String, Backtracking
- Find the Integer Added to Array II Array, Two Pointers, Sorting
- Count Number of Maximum Bitwise-OR Subsets Bit Manipulation, Backtracking
- Reordered Power of 2 Math, Counting
Hard (9)
- Maximum Number of Ways to Partition an Array Array, Hash Table, Counting
- Number of Possible Sets of Closing Branches Bit Manipulation, Graph, Heap (Priority Queue)
- Create Components With Same Value Array, Math, Tree
- Maximum Score of a Node Sequence Array, Graph, Sorting
- Count Increasing Quadruplets Array, Dynamic Programming, Binary Indexed Tree
- Maximum Number of Achievable Transfer Requests Array, Backtracking, Bit Manipulation
- Largest Palindrome Product Math
- Consecutive Numbers Sum Math
- Naming a Company Hash Table, String, Bit Manipulation
Companies that ask enumeration problems
- Amazon 47 problems on enumeration
- Google 37 problems on enumeration
- Microsoft 8 problems on enumeration
- Adobe 5 problems on enumeration
- TCS 3 problems on enumeration
- Accenture 2 problems on enumeration
- Infosys 2 problems on enumeration
- Apple 1 problem on enumeration
- Capgemini 1 problem on enumeration
- Epic Systems 1 problem on enumeration
- Indeed 1 problem on enumeration
- Oracle 1 problem on enumeration
Next topic: Intervals