Practice
1. You are given a binary array and need to find the length of the longest contiguous subarray with an equal number of 0s and 1s. Which algorithmic approach guarantees an optimal O(n) time solution?
easy
Solution
Step 1: Understand problem constraints
The problem requires finding the longest contiguous subarray with equal 0s and 1s efficiently.Step 2: Identify algorithmic pattern
Using prefix sums transformed by treating 0 as -1, and a hash map to store earliest prefix sum indices, allows O(n) time detection of subarrays with zero sum difference.Final Answer:
Option B -> Option BQuick Check:
Prefix sum + hash map is classic for equal 0/1 subarray [OK]
Hint: Prefix sums + hash map track equal 0s and 1s efficiently [OK]
Common Mistakes:
- Thinking greedy can find longest equal 0/1 subarray
- Using DP that tracks counts separately is inefficient
- Brute force is correct but not optimal
2. Consider the following code snippet implementing the optimal approach for longest consecutive sequence. What is the value of
max_len returned when the input is [1, 9, 3, 10, 2]?easy
Solution
Step 1: Identify sequence starts
Numbers are {1,2,3,9,10}. Sequence starts are numbers where num-1 not in set: 1 and 9.Step 2: Count consecutive lengths
From 1: sequence is 1,2,3 -> length 3. From 9: sequence is 9,10 -> length 2. Max length is 3.Final Answer:
Option C -> Option CQuick Check:
Longest consecutive sequence is [1,2,3] length 3 [OK]
Hint: Only count sequences starting at numbers without predecessor [OK]
Common Mistakes:
- Counting sequences from every number causing duplicates
- Off-by-one errors in length counting
- Confusing max length with total unique numbers
3. You are given an integer array and a number
k. You need to find the length of the longest contiguous subarray whose sum is divisible by k. Which of the following approaches guarantees an optimal solution in O(n) time?easy
Solution
Step 1: Understand the problem constraints
We want the longest subarray with sum divisible byk. Checking all subarrays with nested loops isO(n^2)and inefficient.Step 2: Recognize prefix sum + remainder pattern
Using prefix sums modulok, if two prefix sums have the same remainder, the subarray between them is divisible byk. Storing earliest indices of remainders in a hash map allowsO(n)solution.Final Answer:
Option D -> Option DQuick Check:
Prefix sums + hash map is classic for subarray sum modulo problems [OK]
Hint: Prefix sums + remainder hash map -> O(n) solution [OK]
Common Mistakes:
- Thinking sliding window works for modulo sums
- Assuming DP is needed for this problem
- Believing brute force is acceptable for large inputs
4. The following code attempts to find the intersection of two arrays using the two-pointer approach. Which line contains a subtle bug that can cause incorrect results when duplicates exist?
def intersect(nums1, nums2):
nums1.sort()
nums2.sort()
i, j = 0, 0
intersection = []
while i < len(nums1) and j < len(nums2):
if nums1[i] == nums2[j]:
intersection.append(nums1[i])
i += 1
elif nums1[i] < nums2[j]:
i += 1
else:
j += 1
return intersection
medium
Solution
Step 1: Identify the bug
When nums1[i] == nums2[j], only i is incremented but j is not, causing infinite loop or missing duplicates.Step 2: Correct fix
Both i and j must be incremented to move past matched elements and handle duplicates correctly.Final Answer:
Option D -> Option DQuick Check:
Incrementing only one pointer on match breaks correctness [OK]
Hint: Both pointers must advance on match [OK]
Common Mistakes:
- Incrementing only one pointer on match
- Modifying input arrays directly
- Forgetting to sort arrays
5. Suppose the problem changes: now elements can be reused multiple times, and you want the top k elements by total frequency including duplicates. Which modification to the optimal bucket sort approach correctly handles this?
hard
Solution
Step 1: Understand reuse changes frequency dynamics
Elements can appear multiple times dynamically, so frequencies update continuously.Step 2: Bucket sort is static and cannot handle dynamic frequency updates
Min-heap allows maintaining top k elements efficiently as frequencies change.Step 3: Choose min-heap approach for dynamic top k tracking
Min-heap keeps size k and updates with new frequencies, suitable for reuse scenario.Final Answer:
Option C -> Option CQuick Check:
Bucket sort is static; min-heap adapts to frequency changes [OK]
Hint: Dynamic frequency updates require min-heap, not bucket sort [OK]
Common Mistakes:
- Assuming bucket sort works with dynamic reuse
- Counting unique elements only loses frequency info
- Sorting entire array is inefficient
