Practice
nums = [5, 0, 0] and k = 3?Solution
Step 1: Trace running_sum and remainder_map for each iteration
i=0: running_sum=5%3=2, remainder_map={0:-1,2:0} i=1: running_sum=2+0=2%3=2, 2 in map at index 0, i-0=1 (not >1), continue i=2: running_sum=2+0=2%3=2, 2 in map at index 0, i-0=2 (>1), return TrueStep 2: Confirm return value
Function returns True because subarray [0,0] starting at index 1 has sum 0 which is multiple of 3.Final Answer:
Option D -> Option DQuick Check:
Subarray length check and remainder map usage confirm True [OK]
- Off-by-one error in subarray length check
- Ignoring initial remainder 0 at index -1
- Modulo by zero confusion when k=0
p in s. Given s = "abab" and p = "ab", what is the content of res after the loop completes?Solution
Step 1: Initialize frequency counts for
p_count and s_count both have counts for 'a' and 'b' as 1, so matches = 26 initially except for other letters.p = "ab"and first window ins = "ab"Step 2: Slide window over
At r=2, window is "ba" starting at index 1, matches == 26, append 0. At r=3, window is "ab" starting at index 2, matches == 26, append 1. After loop, append last index 2 if matches == 26. Final res = [0,1].sand updateresFinal Answer:
Option D -> Option DQuick Check:
Matches found at indices 0 and 1 for "ab" and "ba" [OK]
- Off-by-one errors in appending indices
- Forgetting last window check
- Miscounting matches after updates
Solution
Step 1: Understand the window shrinking condition
The window should shrink when odd_count > k, not when odd_count >= k, to correctly count subarrays with at most k odd numbers.Step 2: Identify impact of bug
Using '>=' shrinks the window too early, missing valid subarrays and causing undercount.Final Answer:
Option B -> Option BQuick Check:
Changing '>=' to '>' fixes the counting [OK]
- Using '>=' instead of '>' in while condition
- Miscounting subarrays due to early shrinking
- Confusing at_most_k with exactly k logic
Solution
Step 1: Identify loops and operations
The code has two nested loops over n, and inside uses hash map get/set which is average O(1).Step 2: Confirm no hidden factors
Difference range W does not affect complexity since hash maps handle keys efficiently; no sorting or extra loops.Final Answer:
Option D -> Option DQuick Check:
Two nested loops with O(1) hash ops -> O(n^2) time [OK]
- Confusing difference range W as multiplicative factor
- Assuming triple nested loops from brute force
- Thinking sorting is involved
Solution
Step 1: Identify main operations
The algorithm iterates over the array once, updating running_sum and hash map counts.Step 2: Analyze hash map operations
Hash map insertions and lookups are O(1) on average, so total time is O(n).Final Answer:
Option A -> Option AQuick Check:
No nested loops or sorting involved, so O(n) time complexity. [OK]
- Confusing with brute force O(n²)
- Assuming hash map operations are O(log n)
- Counting prefix sum storage as extra time
