Practice
max_len returned when the input is [1, 9, 3, 10, 4, 20, 2]?
def longestConsecutive(nums):
num_set = set(nums)
max_len = 0
for num in num_set:
if num - 1 not in num_set:
current = num
length = 1
while current + 1 in num_set:
current += 1
length += 1
max_len = max(max_len, length)
return max_len
Solution
Step 1: Identify sequence starts
Numbers without predecessor: 1 (0 not in set), 9 (8 not in set), 20 (19 not in set).Step 2: Trace sequences from starts
From 1: sequence is 1,2,3,4 -> length 4; from 9: 9,10 -> length 2; from 20: 20 -> length 1. Max length is 4.Step 3: Re-examine carefully
Check if 1,2,3,4 is the longest. Actually, 1,2,3,4 is length 4, but 1,2,3,4,5 is not present. So max length is 4.Final Answer:
Option B -> Option BQuick Check:
Longest consecutive sequence length is 4 [OK]
- Counting sequences from every number
- Off-by-one in length calculation
- Ignoring duplicates
def subarraySum(nums, k):
count = 0
running_sum = 0
prefix_counts = {0: 1}
for num in nums:
running_sum += num
if running_sum - k in prefix_counts:
count += prefix_counts[running_sum - k]
prefix_counts[running_sum] = prefix_counts.get(running_sum, 0) + 1
return count
print(subarraySum([1, 2, 3], 3))Solution
Step 1: Trace running_sum and prefix_counts
Iteration 1: running_sum=1, prefix_counts={0:1,1:1}, count=0 Iteration 2: running_sum=3, running_sum-k=0 in prefix_counts, count=1, prefix_counts={0:1,1:1,3:1} Iteration 3: running_sum=6, running_sum-k=3 in prefix_counts, count=2, prefix_counts={0:1,1:1,3:1,6:1}Step 2: Return count
Count is 2, representing subarrays [1,2] and [3].Final Answer:
Option D -> Option DQuick Check:
Count matches expected subarrays summing to 3. [OK]
- Off-by-one errors in counting
- Forgetting to initialize prefix_counts with {0:1}
- Miscounting subarrays ending at current index
Solution
Step 1: Understand the role of seen dictionary initialization
Seen must map prefix_sum 0 to index -1 to handle subarrays starting at index 0.Step 2: Identify that initializing seen as empty misses subarrays starting at index 0
Without {0: -1}, the code misses valid subarrays from the start, causing off-by-one errors.Final Answer:
Option A -> Option AQuick Check:
Other lines are correct; only seen initialization causes subtle bugs [OK]
- Not initializing seen with 0:-1
- Overwriting earliest prefix sum indices
- Not transforming 0 to -1
def pivotIndex(nums):
total = sum(nums)
left_sum = 0
for i, num in enumerate(nums):
if left_sum == total - left_sum:
return i
left_sum += num
return -1
Solution
Step 1: Understand the condition
The condition should check if left_sum equals right sum, which is total - left_sum - current num.Step 2: Identify the bug
The code incorrectly checks if left_sum == total - left_sum, missing subtracting the current num, causing wrong pivot detection.Final Answer:
Option D -> Option DQuick Check:
Correct condition is left_sum == total - left_sum - num [OK]
- Forgetting to subtract current num from total
- Misplacing sum updates
- Returning null instead of -1
Solution
Step 1: Understand the problem variant
We want subarrays where sum % k == 0, not sum == k.Step 2: Modify prefix sums to track remainders
Compute prefix sums modulo k and count how many prefix sums have the same remainder, since difference of prefix sums with same remainder is divisible by k.Step 3: Use hash map to count frequencies of remainders
Increment count by frequency of current remainder seen so far.Final Answer:
Option B -> Option BQuick Check:
Sliding window fails with negatives; sorting breaks subarray continuity; original approach doesn't handle divisibility. [OK]
- Trying sliding window with negatives
- Ignoring modulo operation
- Assuming sum equals k and divisible by k are same
