Practice
write after processing the input string S = "ab#c" inside the build function?Solution
Step 1: Trace the build function with input "ab#c"
Start with write=0. Read 'a' -> arr[0]='a', write=1. Read 'b' -> arr[1]='b', write=2. Read '#' -> write=1 (backspace removes 'b'). Read 'c' -> arr[1]='c', write=2.Step 2: Final value of write
After processing, write=2, indicating the final string length is 2 ("ac").Final Answer:
Option A -> Option AQuick Check:
write increments and decrements correctly for backspace [OK]
- Counting '#' as a character and incrementing write
- Not decrementing write on backspace
- Off-by-one errors in write pointer
Solution
Step 1: Understand problem constraints and sorted input
The array is sorted, so for each element, pairs with difference ≤ K lie within a contiguous range to the right.Step 2: Identify optimal approach
A two-pointer sliding window approach can find pairs with difference ≤ K in O(n) time by expanding and contracting the window efficiently.Final Answer:
Option D -> Option DQuick Check:
Two pointers leverage sorted order for linear time counting [OK]
- Assuming binary search is always better than two pointers
- Using DP which is unnecessary and inefficient
- Choosing brute force due to simplicity
Solution
Step 1: Understand the problem constraints
The array is sorted, and duplicates are allowed at most twice, so we can leverage order.Step 2: Identify the approach that uses two pointers and index offset check
This approach compares the current element with the element two positions before the write pointer to decide if it should be written, ensuring O(n) time and O(1) space.Final Answer:
Option B -> Option BQuick Check:
Two pointers with index offset check is the known optimal approach [OK]
- Using nested loops causes O(n²) time
- Hash maps use extra space
- DP is unnecessary here
import bisect
def count_pairs_buggy(nums, K):
count = 0
n = len(nums)
for i in range(n - 1):
target = nums[i] + K
j = bisect.bisect_right(nums, target, i) - 1
if j > i:
count += j - i
return count
Solution
Step 1: Understand bisect_right usage
bisect_right should start searching from i + 1 to avoid counting pairs where j ≤ i.Step 2: Identify bug in line 8
Using i as the start index includes nums[i] itself, causing pairs with i == j or double counting.Final Answer:
Option C -> Option CQuick Check:
Changing i to i + 1 fixes the bug [OK]
- Starting binary search at i instead of i+1
- Off-by-one errors in bisect indices
- Counting pairs where i >= j
Solution
Step 1: Recognize input is unsorted
Two-pointer approach relies on sorted input; it fails if input is unsorted.Step 2: Choose a correct approach
Squaring then sorting with a standard sort is correct and efficient (O(n log n)) for unsorted input.Final Answer:
Option A -> Option AQuick Check:
Sorting after squaring handles unsorted input correctly [OK]
- Applying two-pointer on unsorted input
- Using complex data structures unnecessarily
- Assuming in-place modifications preserve order
