Practice
def reverseString(s):
def helper(left, right):
if left >= right:
return
s[left], s[right] = s[right], s[left]
helper(left + 1, right - 1)
if __name__ == "__main__":
s = ['a', 'b', 'c', 'd']
reverseString(s)
print(s)
Solution
Step 1: Trace helper calls and swaps
Initial call helper(0,3): swap s[0] and s[3] -> ['d','b','c','a'] Next call helper(1,2): swap s[1] and s[2] -> ['d','c','b','a'] Next call helper(2,1): left >= right, stop recursion.Step 2: Final array state
The array after all swaps is ['d','c','b','a'], which is the reversed input.Final Answer:
Option B -> Option BQuick Check:
All pairs swapped correctly, final reversed array matches ['d', 'c', 'b', 'a'] [OK]
- Off-by-one errors in recursion base case
- Swapping wrong indices
- Not swapping at all
water returned by trap([2,0,2])?Solution
Step 1: Initialize variables
left=0, right=2, left_max=0, right_max=0, water=0Step 2: Trace iterations
Iteration 1: height[left]=2, height[right]=2, height[left] not less than height[right], so process right pointer. right_max updated to 2, right moves to 1.
Iteration 2: left=0, right=1, height[left]=2, height[right]=0, height[left] > height[right], process right pointer.
height[right]=0 < right_max=2, water += 2 - 0 = 2, right moves to 0.
Loop ends as left == right.Final Answer:
Option C -> Option CQuick Check:
Water trapped between two height 2 bars with 0 in middle is 2 units [OK]
- Off-by-one errors in pointer movement
- Confusing which pointer to move
- Incorrectly updating max heights
Solution
Step 1: Understand reuse means duplicates allowed in triplets
Allowing reuse means complement can be equal to nums[j], so skipping duplicates must be adjusted.Step 2: Modify hash set approach accordingly
Do not skip duplicates; allow complement == nums[j]; sorting still needed to avoid unordered duplicates.Final Answer:
Option D -> Option DQuick Check:
Allowing complement == nums[j] and no skipping duplicates handles reuse correctly [OK]
- Removing sorting breaks uniqueness
- Using triple loops is inefficient
- Removing duplicate skipping blindly causes duplicates
Solution
Step 1: Understand parity with negatives and zero
In Python,num % 2 == 0correctly identifies even numbers including negatives and zero.Step 2: Confirm no algorithm change needed
The original two-pointer approach works unchanged since parity check is valid for all integers.Final Answer:
Option C -> Option CQuick Check:
Modulo operator handles negative evens and zero correctly [OK]
- Thinking abs() is needed for parity check
- Assuming zeros need special handling
- Believing sorting is required for negatives
Solution
Step 1: Understand reuse requirement
Allowing reuse means pairs can include the same element multiple times, which breaks two-pointer and binary search assumptions.Step 2: Identify suitable approach
Dynamic programming can handle combinations with repeated elements efficiently, unlike two pointers or binary search.Final Answer:
Option C -> Option CQuick Check:
DP handles repeated elements and target sums correctly [OK]
- Trying to reuse pointers incorrectly
- Modifying binary search without correctness
- Assuming brute force is efficient
