Multi-tap keypad: every string a press sequence can spell
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Microsoft and Meta. It tests backtracking, dynamic programming and strings.
An old phone types letters by multi-tap: pressing a key once gives its first letter, twice in a row gives its second letter, and so on. Use this keypad:
| Key | Letters |
|---|---|
| 1 | A B C |
| 2 | D E F |
| 3 | G H I |
| 4 | J K L |
| 5 | M N O |
| 6 | P Q R |
| 7 | S T |
| 8 | U V W |
| 9 | X Y |
| 0 | Z |
You get a string digits of key presses, with no pauses recorded. A run of the same digit is ambiguous: it can be cut into groups in any way, and a group of k presses means the k-th letter of that key. A group can never be longer than the number of letters on its key (no wrap-around), and a group never spans two different digits.
Implement keypad_combinations(digits: str) -> list[str] that returns every string the presses could spell, in sorted order.
Constraints
-
0 <= len(digits) <= 20, each character is0-9. - Return
[]for an empty input. Raise an error for any other character.
Example 1
input: "111"
output: ["AAA", "AB", "BA", "C"]
The run of three 1s can be cut as 1+1+1, 1+2, 2+1 or 3.
Example 2
input: "772"
output: ["SSD", "TD"]
Key 7 has two letters, so its run gives SS or T; the single 2 gives D.
Part 2: return only the number of possible strings for inputs up to 10^5 presses, modulo 10^9 + 7.
Clarifying questions
- Is sorted order plain string order, so
"AAA"comes before"AB"? - Can the output really be that large? Twenty presses of
1give over a hundred thousand strings. Is that acceptable? - Is any digit in a run possible, including
0, which has only one letter? - For Part 2, can the count be computed without building any strings?
What a strong answer covers
- Splits
digitsinto runs of the same digit. Each run is independent, so the answer is the product of the choices per run. - For one run, lists every way to cut its length into groups of size 1 to the key's letter count, and maps each group to its letter.
- Combines the runs with a Cartesian product, then sorts. It notes that the output size, not the algorithm, sets the cost.
- Part 2 counts the cuts of a run of length
Lwith group size at mostcwith a short linear recurrence, and multiplies the counts of the runs modulo10^9 + 7. That is O(len(digits)). - Tests the empty string, a bad character, a run of
0, a run longer than its key ("1111"), and both examples.
Follow-ups
- Keep only strings that are words in a given dictionary, and prune with a trie while generating.
- A pause character that ends a group.
- Allow wrap-around, so four presses of
1giveAagain. - Return the k-th string in sorted order without generating the others.
Where it's asked
What it tests
backtracking · dynamic programming · strings
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.

