The Iterator for Combination interview question asks you to design a class that generates all possible combinations of a given length k from a sorted string of unique characters. You need to implement next() to return the next lexicographical combination and hasNext() to check if any more combinations exist.
Google uses this Iterator for Combination coding problem to test a candidate's mastery of the Iterator interview pattern combined with Backtracking. It evaluates whether you can generate permutations/combinations systematically and manage the "state" of an ongoing search across multiple function calls. It’s a test of lazy vs eager evaluation.
This problem can be solved in two ways:
next() and hasNext() then become simple pointer lookups.next() is called, find the rightmost index that can be incremented and adjust the indices to the right.String: "abc", .
"ab", "ac", "bc".next() returns "ab".next() returns "ac".hasNext() is true.next() a few times, this is extremely inefficient.Always discuss the trade-offs between Eager and Lazy generation. Eager is easier to implement but uses space. Lazy is harder to code but uses only space. Knowing when to use which is a senior-level trait.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Design Compressed String Iterator | Easy | Solve | |
| Split Array into Fibonacci Sequence | Medium | Solve | |
| Additive Number | Medium | Solve | |
| The k-th Lexicographical String of All Happy Strings of Length n | Medium | Solve | |
| Restore IP Addresses | Medium | Solve |