Practice
1. Given the following Python code implementing the optimal approach for longest consecutive sequence, what is the value of
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
easy
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]
Hint: Only count sequences starting at numbers without predecessor [OK]
Common Mistakes:
- Counting sequences from every number
- Off-by-one in length calculation
- Ignoring duplicates
2. What is the time complexity of the optimal hash map with prefix sum remainders approach for the Continuous Subarray Sum problem, assuming the input array length is n and the divisor is k?
medium
Solution
Step 1: Analyze the loop and hash map operations
The algorithm iterates over the array once (O(n)) and performs O(1) average-time hash map lookups and insertions.Step 2: Consider the size of the remainder map
The remainder map size is at most min(n, k), but this does not increase the iteration count beyond O(n).Final Answer:
Option C -> Option CQuick Check:
Single pass with constant-time map ops -> O(n) time [OK]
Hint: Hash map lookups are O(1) average, so linear time [OK]
Common Mistakes:
- Confusing prefix sums with nested loops
- Assuming modulo k adds multiplicative factor
- Ignoring average O(1) hash map operations
3. Consider the following buggy code for finding the longest consecutive sequence. Which line contains the subtle bug that can cause incorrect results on inputs with duplicates?
def longestConsecutive(nums):
num_set = set(nums)
max_len = 0
for num in nums:
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
medium
Solution
Step 1: Identify iteration source
The for loop iterates over nums, which may contain duplicates, causing redundant sequence counting.Step 2: Understand impact of duplicates
Iterating over nums instead of num_set leads to repeated work and incorrect max_len updates, inflating counts.Final Answer:
Option C -> Option CQuick Check:
Changing to iterate over num_set fixes duplicate handling [OK]
Hint: Iterate over unique elements to avoid duplicate counting [OK]
Common Mistakes:
- Iterating over original list with duplicates
- Updating max_len inside inner loop
- Incorrect boundary checks
4. Identify the bug in the following code snippet for the Minimum Operations to Reduce X to Zero problem:
medium
Solution
Step 1: Understand the edge case when target == 0
If target == 0, it means totalSum == x, so removing all elements is needed, so minimum operations = length of array.Step 2: Identify incorrect return value
The code returns -1 incorrectly, which signals no solution, but the correct answer is to remove all elements.Final Answer:
Option A -> Option AQuick Check:
Returning -1 here causes wrong output on full removal case [OK]
Hint: Check edge cases where x equals total sum [OK]
Common Mistakes:
- Returning -1 instead of full length when target == 0
- Misinterpreting no solution vs full removal
- Ignoring edge cases in initial checks
5. Suppose the problem is modified so that the input array can contain duplicate numbers and you want to find the length of the longest consecutive sequence considering duplicates as separate elements (e.g., [1,2,2,3] counts as length 4). Which modification to the optimal hash set approach correctly handles this variant?
hard
Solution
Step 1: Understand the variant
Duplicates count as separate elements, so sequences can include repeated numbers increasing length.Step 2: Evaluate approaches
Original hash set approach ignores duplicates; multiplying length by counts is incorrect because duplicates may be non-consecutive; sorting and scanning works but is O(n log n).Step 3: Correct approach
Using a hash map to store counts allows summing counts of consecutive numbers to find the longest run including duplicates efficiently.Final Answer:
Option A -> Option AQuick Check:
Hash map counting handles duplicates correctly and efficiently [OK]
Hint: Count duplicates explicitly using a hash map [OK]
Common Mistakes:
- Ignoring duplicates
- Multiplying length by counts incorrectly
- Assuming sorting is always best
