Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Phone Number Combinations Recursion

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Phone Number Combinations Recursion. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

The Uber app may need to generate keypad-style text suggestions from a sequence of digits. Given a string containing digits from 2 through 9, return every possible letter combination represented by those digits using the standard phone keypad mapping.

Use a recursive backtracking algorithm. The result must preserve keypad order: choices for each digit should be explored from left to right. Return an empty list when the input string is empty.

Formal Specification

Implement letter_combinations(digits), where digits is a string and the return value is a list of strings. Each output string must contain exactly one letter for every input digit, with letters selected from the corresponding keypad group.

Mapping: 2 = abc, 3 = def, 4 = ghi, 5 = jkl, 6 = mno, 7 = pqrs, 8 = tuv, 9 = wxyz.

Constraints

  • 0 <= len(digits) <= 10
  • Every character in digits is an ASCII digit from 2 through 9
  • Return combinations in keypad order

Function Signature

def letter_combinations(digits):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output