Practice
1. Given the following code, what is the return value when the input string is
"aabc"?easy
Solution
Step 1: Trace frequency and first_index dictionaries
For "aabc": freq={'a':2, 'b':1, 'c':1}, first_index={'a':0, 'b':2, 'c':3}Step 2: Find minimum index with frequency 1
Characters with count 1 are 'b' at index 2 and 'c' at index 3; minimum is 2.Final Answer:
Option A -> Option AQuick Check:
First unique char is 'b' at index 2 [OK]
Hint: Check frequency and first occurrence carefully [OK]
Common Mistakes:
- Returning character instead of index
- Off-by-one index errors
- Ignoring multiple unique chars
2. Consider the following Python code implementing the optimal bucket sort approach for top K frequent elements. What is the value of
res just before the function returns when called with nums = [1,2,2,3] and k = 2?easy
Solution
Step 1: Calculate frequency map for nums=[1,2,2,3]
freq = {1:1, 2:2, 3:1}, max_freq = 2Step 2: Fill buckets and collect top k=2 elements
buckets = [[], [1,3], [2]]; iterate from freq=2 down: add 2, then freq=1 add 1; res = [2,1]Final Answer:
Option B -> Option BQuick Check:
Order depends on bucket insertion; here 2 appears before 1 [OK]
Hint: Trace buckets from highest frequency down [OK]
Common Mistakes:
- Confusing order of elements in buckets
- Assuming sorted order inside buckets
- Off-by-one in frequency indexing
3. Consider the following Python code implementing the Two Sum problem using sorting and two pointers. What is the output when calling
two_sum([3, 2, 4], 6)?easy
Solution
Step 1: Sort nums with indices
nums_with_index = [(0,3), (1,2), (2,4)] sorted by value -> [(1,2), (0,3), (2,4)]Step 2: Two pointers check sums
left=0 (value=2), right=2 (value=4), sum=6 equals target -> return indices [1, 2]Final Answer:
Option D -> Option DQuick Check:
Sum found at indices 1 and 2 matching target 6 [OK]
Hint: Sort with indices, then two pointers find correct pair [OK]
Common Mistakes:
- Returning values instead of indices
- Mixing up left and right indices
- Off-by-one errors in pointer movement
4. Suppose the problem changes: now you want to detect if any number appears at least twice, but the array is extremely large and cannot fit into memory. Which approach is best suited to handle this scenario?
hard
Solution
Step 1: Understand memory constraints
The array is too large to fit in memory, so hash sets or sorting in memory are infeasible.Step 2: Evaluate options
External sorting is possible and practical for large data sets that cannot fit into memory. Bloom filters provide probabilistic detection but can yield false positives, which may not be acceptable depending on requirements. Nested loops are impractical due to O(n²) time complexity.Step 3: Choose external sorting
External sorting allows sorting data on disk and then scanning sequentially for duplicates, which is efficient and exact.Final Answer:
Option A -> Option AQuick Check:
External sorting handles large data sets with limited memory and provides exact duplicate detection [OK]
Hint: External sorting enables exact duplicate detection on large data sets [OK]
Common Mistakes:
- Assuming hash sets can handle huge data
- Ignoring external sorting costs
- Choosing nested loops despite inefficiency
- Overlooking bloom filter false positives
5. Suppose the array
nums can contain negative numbers and you want to count subarrays summing to k. Which modification to the prefix sum + hash map approach correctly handles this scenario?hard
Solution
Step 1: Understand sliding window limitations
Sliding window fails with negative numbers because sums can decrease unpredictably.Step 2: Verify prefix sum + hash map suitability
Prefix sums with hash map count frequencies and handle negative numbers naturally without sorting.Step 3: Evaluate other options
Sorting breaks subarray continuity; nested loops are correct but inefficient.Final Answer:
Option A -> Option AQuick Check:
Prefix sum + hash map is the robust solution for negatives [OK]
Hint: Prefix sums + hash map handles negatives; sliding window does not [OK]
Common Mistakes:
- Trying sliding window with negatives
- Sorting array breaks subarray continuity
- Assuming prefix sums fail with negatives
