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. What is the time complexity of the optimal sliding window algorithm that uses two frequency arrays and a match count to find all anagrams of
p in s?medium
Solution
Step 1: Analyze the main loop over string
The algorithm slides a window of sizesm = len(p)oversonce, performing O(1) updates per step.Step 2: Consider frequency array operations
Frequency arrays are fixed size 26, so comparisons and updates are O(1) per iteration, not dependent onm.Final Answer:
Option C -> Option CQuick Check:
Sliding window with fixed alphabet size -> O(n) time [OK]
Hint: Fixed alphabet size means frequency ops are O(1) [OK]
Common Mistakes:
- Confusing frequency array size with input size
- Assuming nested loops cause O(n*m)
- Ignoring constant alphabet size
4. Suppose the problem is modified so that elements can be reused multiple times in the subsequence (i.e., indices can be repeated). Which modification to the DP approach correctly handles this variant?
hard
Solution
Step 1: Understand reuse means indices can repeat
Standard DP assumes strictly increasing indices; allowing reuse breaks this assumption.Step 2: Recognize recursive memoization can explore all sequences with repeated elements
Recursive approach with memoization can try all next elements including current, correctly handling reuse.Step 3: Evaluate other options
Allowing j == i breaks DP indexing logic; resetting dp[i][diff] loses subsequence length info; BFS is inefficient and complex.Final Answer:
Option A -> Option AQuick Check:
Recursive memoization handles repeated indices correctly [OK]
Hint: Reuse requires recursion with memoization, not standard DP indexing [OK]
Common Mistakes:
- Allowing j == i in DP loops breaks monotonic index assumption
- Resetting dp values loses subsequence length info
- Trying BFS which is inefficient
5. Suppose the problem is modified so that elements can be removed multiple times (i.e., you can reuse elements from the ends infinitely) to reduce x to zero. Which approach correctly adapts to this variant?
hard
Solution
Step 1: Understand the reuse variant
Since elements can be reused infinitely, the problem becomes similar to coin change where you want minimum coins (elements) summing to x.Step 2: Recognize sliding window limitation
Sliding window relies on contiguous subarrays without reuse; it cannot handle infinite reuse.Step 3: Use DP approach
Dynamic programming can find minimum removals with unlimited reuse by building solutions for all sums up to x.Final Answer:
Option A -> Option AQuick Check:
DP handles reuse, sliding window does not [OK]
Hint: Reuse implies DP coin change pattern [OK]
Common Mistakes:
- Assuming sliding window works with reuse
- Ignoring infinite reuse changes problem nature
- Using greedy which fails on some sums
