By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(A Complete FAANG-Level Interview Guide)
"This problem tests your ability to generate all possible combinations efficiently—a pattern that appears in 1 out of every 3 onsite interviews at FAANG. Master it, and you’ll prove you can handle backtracking, recursion, and edge cases under pressure."
Before tackling this problem, ensure you understand: 1. Backtracking (DFS) – Recursively exploring all possible paths and backtracking when a path is exhausted. 2. Hash Maps (Dictionaries) – Mapping digits to their corresponding letters (e.g., '2' → ['a', 'b', 'c']). 3. String Manipulation – Concatenating characters and building strings dynamically.
'2' → ['a', 'b', 'c']
"23"
'a' + 'd'
'a' + 'e'
'a' + 'f'
'b'
""
'a'
'c'
'd'
'e'
'f'
digit_map = {'2': 'abc', '3': 'def', ...}
'1'
[]
O(n)
O(4^n)
(Repeatable mental checklist for every problem of this type.)
["ad","ae","af","bd","be","bf","cd","ce","cf"]
Edge cases: Empty string (""), digits '0' or '1' (no letters).
'0'
Precompute Digit-to-Letter Mapping
Create a dictionary: python digit_map = { '2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz' }
python digit_map = { '2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz' }
Choose an Approach: Backtracking (DFS) or BFS
BFS (Iterative):
Implement the Chosen Approach
BFS:
Handle Edge Cases
If input contains '0' or '1', skip (no letters).
Optimize Time & Space
"777"
4^3 = 64
Space: O(n) (DFS recursion stack) or O(4^n) (BFS queue).
Test with Examples
"2"
"79"
'7'
'9'
Problem: Given "23", return all possible letter combinations.
Thought Process: 1. Map '2' → 'abc', '3' → 'def'. 2. Start with "", then append 'a', 'b', 'c' recursively. 3. For each, append 'd', 'e', 'f' and add to result.
'2'
'abc'
'3'
'def'
Code (Python):
def letterCombinations(digits): if not digits: return [] digit_map = { '2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz' } result = [] def backtrack(index, current_combination): if index == len(digits): result.append(current_combination) return current_digit = digits[index] for letter in digit_map[current_digit]: backtrack(index + 1, current_combination + letter) backtrack(0, "") return result
Time Complexity: O(4^n) (each digit has up to 4 letters, and we explore all combinations). Space Complexity: O(n) (recursion stack depth = number of digits).
Why This Works: - Backtracking explores all paths by appending each letter of the current digit and recursing. - The base case (index == len(digits)) ensures we only add complete combinations.
index == len(digits)
Problem: Same as above, but implement using BFS (queue).
Thought Process: 1. Start with "" in the queue. 2. For each digit, dequeue all current combinations, append each letter, and enqueue. 3. Repeat until all digits are processed.
from collections import deque def letterCombinations(digits): if not digits: return [] digit_map = { '2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz' } queue = deque([""]) result = [] for digit in digits: level_size = len(queue) for _ in range(level_size): current = queue.popleft() for letter in digit_map[digit]: queue.append(current + letter) return list(queue)
Time Complexity: O(4^n) (same as DFS). Space Complexity: O(4^n) (queue stores all combinations at each level).
Why This Works: - BFS builds combinations level by level, ensuring all possibilities are explored. - The queue dynamically grows as we append letters, then shrinks as we process them.
Problem: Given "23", return combinations without duplicates (e.g., if '2' maps to 'aab' instead of 'abc').
'aab'
Thought Process: 1. Modify digit_map to allow duplicate letters (e.g., '2': 'aab'). 2. Use a set to avoid duplicate combinations. 3. Prune early if a letter has already been used in the current path.
digit_map
'2': 'aab'
def letterCombinations(digits): if not digits: return [] digit_map = { '2': 'aab', # Example with duplicates '3': 'def' } result = set() # Avoid duplicates def backtrack(index, current_combination): if index == len(digits): result.add(current_combination) return current_digit = digits[index] for letter in digit_map[current_digit]: backtrack(index + 1, current_combination + letter) backtrack(0, "") return list(result)
Time Complexity: O(4^n) (worst case, but pruning reduces actual runtime). Space Complexity: O(n) (recursion stack) + O(4^n) (set storage).
Why This Works: - Set ensures no duplicates in the result. - Backtracking still explores all paths, but the set filters out repeats.
if not digits: return []
for
'0'?" | Interviewer asks about edge cases. | Explicitly handle
and
"Alright, let’s lock this in. Here’s your 60-second recap:
Now go crush that interview. You’ve got this!
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.