Data Structures & AlgorithmsModerateProduct Company SDE · Amazon/Microsoft-style OA

Problem: Generate the letter combinations a phone keypad number can represent and report how many there are.

Problem: Generate the letter combinations a phone keypad number can represent and report how many there are.
from itertools import product
keys = {'2': 'abc', '3': 'def', '7': 'pqrs', '9': 'wxyz'}
def combos(d):
    return [''.join(p) for p in product(*(keys[c] for c in d))] if d else []
r = combos("279")
print(len(r), r[5], len(combos("")))
What is printed?
  1. A. 48 aqx 0
  2. B. 48 aqy 0
  3. C. 36 aqx 0
  4. D. 48 aqx 1

Correct answer: A. 48 aqx 0

3 × 4 × 4 = 48 combinations, generated like an odometer (last digit fastest): apw, apx, apy, apz, aqw, aqx… so index 5 is 'aqx'. An empty input must return an empty list, not [""]. Output size bounds the time: O(4ⁿ · n).

More Coding Interview Problems questions

Exams that ask this topic