Skip to content

Interview Solver

FeaturesPricingHelp
Sign In
FeaturesPricingHelpSign In
  1. Interview Questions
  2. /
  3. Problems
  4. /
  5. Letter Combinations of a Phone Number

Letter Combinations of a Phone Number

List every word a string of phone keypad digits could spell. A clean first exercise in backtracking: pick one letter per digit, recurse, undo. Solve it below in Python or JavaScript, then read the walkthrough.

MediumHash TableStringBacktrackingAsked at 124 companies incl. Google, Amazon, Meta

Problem

On a phone keypad, the digits 2 through 9 each stand for a few letters:

DigitLettersDigitLetters
2abc6mno
3def7pqrs
4ghi8tuv
5jkl9wxyz

Given a string digits made of the characters 2–9, return every string you can make by choosing one letter for each digit, keeping the digits in order.

Return the strings in any order. If digits is empty, return an empty list.

Example 1

Input
digits = "38"
Output
["dt", "du", "dv", "et", "eu", "ev", "ft", "fu", "fv"]

3 gives d, e or f; 8 gives t, u or v. That is 3 × 3 = 9 strings.

Example 2

Input
digits = "7"
Output
["p", "q", "r", "s"]

Example 3

Input
digits = ""
Output
[]

No digits means nothing to spell, so the list is empty.

Constraints

  • 0 ≤ digits.length ≤ 4
  • digits[i] is one of '2' through '9'.

Practice

1
2
3
4

Runs 10 tests in your browser. Python loads on the first run (about 6 MB, then cached). Press ⌘/Ctrl + Enter to run.

Get asked Letter Combinations of a Phone Number in a real interview?

Interview Solver gives you a working solution live, during your actual coding interview. Invisible to screensharing.

Start Free Trial

How to solve Letter Combinations of a Phone Number

Pattern: Backtracking over choices

"Return every combination" is the signal for backtracking. Each digit is a decision with a few options, and the decisions are independent, so the answers are the leaves of a tree: the root is the empty string, each level adds one letter for the next digit, and the depth equals len(digits).

You can't do this with a fixed number of nested loops because the number of digits varies. Recursion gives you exactly one "loop" per digit: at position i, try each letter for digits[i], recurse on i + 1, then undo the choice. When i reaches the end, the current path is a complete answer.

Approach 1: Iterative expansion

Start with [""]. For each digit, replace the list with every existing string extended by each of that digit's letters. Simple and correct; it builds every intermediate level in memory, which is fine at this size.

Time O(4ⁿ · n) · Space O(4ⁿ · n)

Approach 2: Backtracking with a path

  1. If digits is empty, return [].
  2. Define backtrack(i): if i == len(digits), add "".join(path) to the results and return.
  3. Otherwise, for each letter mapped to digits[i]: push it onto path, call backtrack(i + 1), then pop it.

Reusing one path list means each answer is built once, at the leaf.

Time O(4ⁿ · n) · Space O(n) recursion, plus the output

ApproachTimeSpace
Iterative expansionO(4ⁿ · n)O(4ⁿ · n)
Backtracking with a pathO(4ⁿ · n)O(n) recursion, plus the output

Solution code

Try the practice editor first. Both solutions pass every test above.

▸Letter Combinations of a Phone Number solution in Python
KEYPAD = {
    "2": "abc", "3": "def", "4": "ghi", "5": "jkl",
    "6": "mno", "7": "pqrs", "8": "tuv", "9": "wxyz",
}


def letter_combinations(digits: str) -> list[str]:
    if not digits:
        return []
    results = []
    path = []

    def backtrack(i: int) -> None:
        if i == len(digits):
            results.append("".join(path))
            return
        for letter in KEYPAD[digits[i]]:
            path.append(letter)
            backtrack(i + 1)
            path.pop()

    backtrack(0)
    return results
▸Letter Combinations of a Phone Number solution in JavaScript
const KEYPAD = {
  2: 'abc', 3: 'def', 4: 'ghi', 5: 'jkl',
  6: 'mno', 7: 'pqrs', 8: 'tuv', 9: 'wxyz'
};

function letterCombinations(digits) {
  if (!digits) return [];
  const results = [];
  const path = [];

  const backtrack = (i) => {
    if (i === digits.length) {
      results.push(path.join(''));
      return;
    }
    for (const letter of KEYPAD[digits[i]]) {
      path.push(letter);
      backtrack(i + 1);
      path.pop();
    }
  };

  backtrack(0);
  return results;
}

Common mistakes

  • Returning `[""]` instead of `[]` for empty input. Both the iterative and recursive versions produce `[""]` unless you check first.
  • Forgetting that 7 and 9 have four letters, not three.
  • Appending the shared `path` list itself to the results instead of a joined copy, so every result changes when you backtrack.
  • Indexing the mapping by digit value with an off-by-two array (digit 2 is the first entry).

What interviewers look for

Describe the decision tree before you code: one level per digit, one branch per letter, answers at the leaves. That framing tells the interviewer you can recognize backtracking, and it gives you the complexity for free: up to 4 branches per level and n levels, with O(n) work to build each leaf.

Handle the empty input first and say so out loud. Then write the choose / recurse / undo structure clearly, because it's the template for harder problems like permutations, subsets and N-Queens.

Follow-up questions

How would you return only combinations that are real words?

Put the dictionary in a trie and walk it during the backtracking. If the current prefix is not in the trie, prune that branch immediately instead of finishing the string.

How would you do it without recursion?

Use the iterative expansion with a queue: pop a partial string, and if it is shorter than digits, push it extended by each letter of the next digit. Strings of full length are the answers.

What if digits 0 and 1 can appear and map to no letters?

Decide the rule with the interviewer. Usually those digits are skipped, so you would filter them out before backtracking; if they must produce something, map each to itself as a single character.

Companies that ask Letter Combinations of a Phone Number

Letter Combinations of a Phone Number is tagged by 124 companies in our interview dataset. The score shows how often it comes up at each company.

  • Deezer100%
  • Offerup100%
  • Payactiv94%
  • Dropbox92.1%
  • Abbott90%
  • Kroger90%
  • Gemini89%
  • Trexquant89%
  • Epic Systems88.4%
  • Adidas85%
  • Thryv84%
  • Hudl81%
  • Nexhealth80%
  • Gigamon78%
  • Elevance Health75%
  • The D E Shaw Group74.7%
  • Red Hat74%
  • Nasa Jpl74%
  • LinkedIn73.6%
  • Unanet73%
  • Jabil72%
  • Atos71%
  • Accenture70.8%
  • Flexport70.7%

and 100 more.

Practice next

  • Longest Substring Without Repeating CharactersMediumSliding window with last-seen index
  • Group AnagramsMediumHash map keyed by a canonical form
  • Roman to IntegerEasyCompare each symbol with the next
  • Word LadderHardBreadth-first search on an implicit graph

Get live help in the real interview

Working solutions in real time during your actual interview. Invisible to screensharing.

Start Free Trial

No credit card required

Interview Solver
  • Home
  • About
  • Pricing
  • Sign in
  • Contact
  • Blog
  • Companion
  • Mobile
  • Help Center
  • Developers
  • Use Cases
  • Software Engineer
  • Product Manager
  • Misc
  • Privacy Policy
  • Terms and Conditions
  • Refund & Cancellation
  • Discord Community
  • Affiliate Program
  • Compare
  • All Comparisons
  • vs Final Round AI
  • vs UltraCode
  • vs Interview Coder
  • vs LockedIn AI
  • vs AI Apply
  • Free Tools
  • ATS Resume Checker
  • STAR Method Builder
  • Salary Lookup
  • Big O Cheat Sheet
  • Total Comp Calculator
  • View All →
  • Interview Questions
  • Google Questions
  • Amazon Questions
  • Microsoft Questions
  • Meta Questions
  • Apple Questions
  • TikTok Questions
  • View All →
© 2026 Interview Solver, Inc. All rights reserved.