Practice
1. Consider the following Python function to find the longest contiguous subarray with equal number of 0s and 1s. What is the returned value when the input is
[0, 1, 0, 1]?
def findMaxLength(nums):
prefix_sum = 0
seen = {0: -1}
max_length = 0
for i, num in enumerate(nums):
prefix_sum += 1 if num == 1 else -1
if prefix_sum in seen:
max_length = max(max_length, i - seen[prefix_sum])
else:
seen[prefix_sum] = i
return max_length
easy
Solution
Step 1: Trace prefix sums and seen map
Index 0: num=0 -> prefix_sum=-1, seen={0:-1, -1:0}, max_length=0 Index 1: num=1 -> prefix_sum=0, seen has 0 at -1, max_length=max(0,1-(-1))=2 Index 2: num=0 -> prefix_sum=-1, seen has -1 at 0, max_length=max(2,2-0)=2 Index 3: num=1 -> prefix_sum=0, seen has 0 at -1, max_length=max(2,3-(-1))=4Step 2: Return max_length
max_length is 4, the length of the entire array.Final Answer:
Option A -> Option AQuick Check:
Full array balanced, max_length=4 [OK]
Hint: Prefix sums track balance; max length updates on repeated sums [OK]
Common Mistakes:
- Off-by-one errors in length calculation
- Ignoring initial seen entry for sum 0
- Confusing prefix sum updates
2. Consider the following buggy code for detecting if a number is happy. Which line contains the subtle bug that can cause incorrect results or infinite loops?
def sum_of_squares(num):
return sum(int(d) ** 2 for d in str(num))
UNHAPPY_CYCLE = {4, 16, 37, 58, 89, 145, 42, 20}
def isHappy(n: int) -> bool:
while n != 1 and n not in UNHAPPY_CYCLE:
n = sum_of_squares(n)
return true
medium
Solution
Step 1: Analyze return statement
The function returns true unconditionally after loop ends, ignoring if n is in unhappy cycle.Step 2: Identify correct return logic
Should return n == 1 to distinguish happy from unhappy numbers.Final Answer:
Option A -> Option AQuick Check:
Incorrect return causes false positives [OK]
Hint: Return must check if n == 1 after loop [OK]
Common Mistakes:
- Returning true always after loop
- Ignoring unhappy cycle detection
3. The following code attempts to find the longest subarray with sum divisible by
k. Identify the line containing the subtle bug that causes incorrect results for arrays containing negative numbers.
def longest_subarray_div_by_k(nums, k):
earliest = [-2] * k
earliest[0] = -1
max_length = 0
prefix_sum = 0
for i, num in enumerate(nums):
prefix_sum += num
remainder = prefix_sum % k
# Bug: missing adjustment for negative remainder
if earliest[remainder] != -2:
max_length = max(max_length, i - earliest[remainder])
else:
earliest[remainder] = i
return max_length
medium
Solution
Step 1: Identify remainder calculation
When prefix_sum is negative,prefix_sum % kin Python returns a non-negative remainder, but in some languages or implementations it can be negative. The code lacks adjustment for negative remainder.Step 2: Why this causes bugs
If remainder is negative, indexing earliest array causes errors or misses correct subarrays. The fix is to addif remainder < 0: remainder += k.Final Answer:
Option A -> Option AQuick Check:
Negative prefix sums require remainder adjustment [OK]
Hint: Check remainder calculation for negative sums [OK]
Common Mistakes:
- Forgetting to adjust negative remainders
- Misinitializing earliest array
- Updating max_length incorrectly
4. What is the time complexity of the
get operation in the optimal time-based key-value store implementation that uses a hash map with sorted timestamp lists and binary search?medium
Solution
Step 1: Identify the data structure used for retrieval
Retrieval uses binary search on a sorted list of timestamps for the given key.Step 2: Analyze binary search complexity
Binary search on n timestamps takes O(log n) time. Hash map lookup is O(1) average but the dominant cost is binary search.Final Answer:
Option A -> Option AQuick Check:
Binary search on timestamps list dominates get operation time [OK]
Hint: Binary search on timestamps list is O(log n) [OK]
Common Mistakes:
- Confusing total keys with timestamps count
- Assuming hash map makes get O(1) ignoring binary search
- Mistaking linear search complexity for get
5. What is the time complexity of the optimal frequency counting approach for checking if two strings of length n are anagrams, assuming the strings contain only lowercase English letters?
medium
Solution
Step 1: Identify loops over input strings
Both strings are traversed once, each iteration doing O(1) work updating counts.Step 2: Analyze frequency array operations
Frequency array is fixed size 26, so updates are constant time, not multiplied by n.Final Answer:
Option B -> Option BQuick Check:
Overall time is linear in string length n [OK]
Hint: Frequency updates are O(1), total O(n) time [OK]
Common Mistakes:
- Confusing sorting complexity with counting
- Multiplying by 26 unnecessarily
- Assuming constant time overall
