Practice
def addOperators(num: str, target: int) -> List[str]:
res = []
path = []
def backtrack(index: int, evaluated: int, last_operand: int):
if index == len(num):
if evaluated == target:
res.append(''.join(path))
return
for i in range(index, len(num)):
# Bug: missing leading zero check
curr_str = num[index:i+1]
curr = int(curr_str)
length_before = len(path)
if index == 0:
path.append(curr_str)
backtrack(i+1, curr, curr)
path[length_before:] = []
else:
path.append('+'); path.append(curr_str)
backtrack(i+1, evaluated + curr, curr)
path[length_before:] = []
backtrack(0, 0, 0)
return res
Solution
Step 1: Identify handling of leading zeros
The code lacks the check 'if i != index and num[index] == '0': break' which prevents invalid operands like "05".Step 2: Confirm bug location
This missing check is critical and should be placed before parsing curr_str to avoid invalid expressions.Final Answer:
Option A -> Option AQuick Check:
Missing leading zero check causes invalid operands [OK]
- Confusing backtracking cleanup lines as bug
- Thinking substring extraction is buggy
from collections import deque
def letterCombinations(digits):
if digits == '':
return ['']
digit_map = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl',
'6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}
queue = deque([''])
for digit in digits:
letters = digit_map[digit]
for _ in range(len(queue)):
prev = queue.popleft()
for char in letters:
queue.append(prev + char)
return list(queue)
Solution
Step 1: Analyze empty input handling
Returning [''] for empty input is incorrect; the problem expects an empty list [] when input is empty.Step 2: Check other lines
Digit mapping, queue operations, and imports are correct and do not cause bugs.Final Answer:
Option B -> Option BQuick Check:
Empty input should return [] not [''] to match problem spec [OK]
- Returning [''] instead of [] for empty input
- Assuming digits '1' or '0' map to letters
def permute(nums):
res = [nums[:]]
c = [0] * len(nums)
i = 0
while i < len(nums):
if c[i] < i:
if i % 2 == 0:
nums[0], nums[i] = nums[i], nums[0]
else:
nums[c[i]], nums[i] = nums[i], nums[c[i]]
res.append(nums)
c[i] += 1
i = 0
else:
c[i] = 0
i += 1
return res
What is the bug in this code?Solution
Step 1: Identify how results are stored
The code appendsnumsdirectly toreswithout copying.Step 2: Understand consequences of appending references
Sincenumsis mutated in-place, all entries inrespoint to the same list, resulting in duplicates of the final permutation.Final Answer:
Option B -> Option BQuick Check:
Appending a copynums[:]fixes the bug [OK]
- Forgetting to copy before appending results in duplicate references
Solution
Step 1: Identify branching factor per segment
Each segment can be length 1, 2, or 3, so branching factor is at most 3 per segment.Step 2: Calculate total combinations
There are 4 segments, so total combinations are at most 3^4 = 81. Pruning further reduces this.Final Answer:
Option A -> Option AQuick Check:
Time complexity depends on fixed 4 segments with max 3 choices each [OK]
- Assuming complexity depends on n^4
- Confusing permutations with combinations
Solution
Step 1: Understand the problem variant
Elements can be reused unlimited times, so no need to track usage.Step 2: Modify backtracking accordingly
Removing the used array allows choosing any element at each recursion level multiple times, generating permutations with repetition.Final Answer:
Option A -> Option AQuick Check:
Allowing repeated choices without usage tracking fits unlimited reuse [OK]
- Keeping used array causes missing permutations or incorrect pruning
