← Questions

Letter combinations of a phone number

0
UberCodingLast asked Mar 2026

A coding interview question asked in Uber's technical phone screen, last asked Mar 2026. It tests backtracking and strings.

On a phone keypad each digit from 2 to 9 stands for a few letters: 2 → abc, 3 → def, 4 → ghi, 5 → jkl, 6 → mno, 7 → pqrs, 8 → tuv, 9 → wxyz. Given a string of digits, list every letter string it can spell. Then count them without listing them.

Part 1: list the combinations

Unless you agree on a different interface with the interviewer, assume:

  • letter_combinations(digits: str) -> list[str] — every string formed by choosing one letter per digit, in backtracking order: digits from left to right, each digit's letters in keypad order. An empty input returns [].
letter_combinations("23")  ->  ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
letter_combinations("")    ->  []

Part 2: count them

  • count_combinations(digits: str) -> int — the number of strings Part 1 would return, without building them. An empty input returns 0.
count_combinations("2345")  ->  81    # 3 × 3 × 3 × 3
count_combinations("79")    ->  16    # 4 × 4

Constraints

  • Part 1: 0 <= len(digits) <= 10. Part 2: 0 <= len(digits) <= 10^5.
  • Every character is between 2 and 9.

Clarifying questions

  • Can the input contain 0, 1, * or #? What should they do?
  • Does the output order matter?
  • For the count, is the exact big number needed, or the count modulo some prime?

What a strong answer covers

  • Backtracking: one recursion level per digit, append a letter, recurse, remove it. Or an iterative build that extends every partial string by each letter.
  • Cost: O(4^n · n) time, since there are up to 4^n strings of length n; the output size dominates.
  • Part 2 as a product of each digit's letter count, O(n), and why generating would be hopeless for 10^5 digits.
  • Tests: empty input, a single digit, digits with four letters.

Follow-ups

  • Return only combinations that are words in a given dictionary. How do you prune early (a trie of the dictionary)?
  • Return the k-th combination in order without generating the earlier ones.
  • Count modulo 10^9 + 7 for very long inputs.

Where it's asked

  • Uber · Technical phone screen · Last asked Mar 202660 min · One medium problem with a follow-up, or two short ones; sometimes object-oriented design

What it tests

backtracking · strings

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding