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?- A. 48 aqx 0
- B. 48 aqy 0
- C. 36 aqx 0
- 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
- Problem: Count set bits for each number 0..n using DP on the lowest bit. python def bits(n): r = [0] (n + 1) for i in range(1, n + 1)…
- Problem: Check whether a number is a power of two and isolate its lowest set bit. python print("; ".join(f"{x > 0 and x & (x - 1) == 0} {x…
- Problem: Every value appears three times except one, which appears once. Find it with O(1) extra space. python def single(a): ones = twos…
- Problem: Two values appear once and every other value twice. Find both (smaller first). python def twosingle(a): x = 0 for v in a: x ^= v…
- Problem: Count the bits that differ between two IDs (Hamming distance) and the total over all pairs of an array. python def ham(x, y)…
- Problem: A permissions mask is an int; report how many permissions (set bits) each mask grants. java static int popc(int x) { return…