📖 2 min read
Last updated on

Letter Combinations of a Phone Number — Coding Interview Walkthrough


You’re in a coding interview. The interviewer says: “Given a string of digits 2–9, return all possible letter combinations from a phone keypad.” You’re building the Cartesian product of letter-sets — one per digit. Backtracking models this naturally.


The Problem

Given a string containing digits from 2-9, return all possible letter combinations. Mapping: 2→abc, 3→def, 4→ghi, 5→jkl, 6→mno, 7→pqrs, 8→tuv, 9→wxyz.

Example

Input: digits = "23" Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"]

Already comfortable with the solution? Practice it in a mock interview →

Decision tree for phone digits '23': digit 2 maps to a,b,c; digit 3 maps to d,e,f. All 9 combinations shown at leaf level.

What Interviewers Are Actually Testing

SkillWhat Interviewers Observe
Backtracking structureDo you use “build, recurse, undo” cleanly?
Base caseDo you add to results when path length equals digit count?
Empty inputDo you return [] for empty digits?

Step 1: The Key Insight

The problem is the Cartesian product of multiple letter-sets. At depth i, pick each letter for digits[i], recurse to i+1, and undo.


Python Implementation

def letterCombinations(digits: str) -> list[str]:
    if not digits:
        return []

    phone_map = {
        '2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl',
        '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'
    }

    results = []
    current = []

    def backtrack(index: int) -> None:
        if index == len(digits):
            results.append(''.join(current))
            return
        for letter in phone_map[digits[index]]:
            current.append(letter)
            backtrack(index + 1)
            current.pop()

    backtrack(0)
    return results

Time & Space Complexity

AspectComplexity
TimeO(4ⁿ × n) — some keys have 4 letters
SpaceO(n) — recursion stack

Common Interview Mistakes

  1. Not handling empty input. Return [] for digits = "".
  2. String concatenation instead of list. Can’t efficiently undo +=. Use list + pop().
  3. Forgetting the undo step. current.pop() after recursion is essential.

What a Strong Candidate Sounds Like

“Backtracking over a decision tree. At each depth I choose a letter for the current digit. I maintain a path list, recurse, and pop after each call. Time is O(4ⁿ × n).”



Practice This in a Mock Interview

Start a mock interview for Letter Combinations on Intervu.


Further reading:

Practice Like It's the Real Interview

Get instant feedback on your approach, communication, and code — powered by AI.

Start a Mock Interview →