Practice
1. You are given an integer array and a target integer. The task is to find three integers in the array whose sum is closest to the target. Which approach guarantees an optimal solution with O(n²) time complexity?
easy
Solution
Step 1: Understand problem constraints
The problem requires finding three numbers whose sum is closest to a target, which suggests checking combinations efficiently.Step 2: Identify approach with O(n²) complexity
Sorting the array allows fixing one element and using two pointers to find pairs that sum closest to the remaining target, achieving O(n²) time.Final Answer:
Option A -> Option AQuick Check:
Sorting + two pointers is classic for 3Sum Closest [OK]
Hint: Sorting + two pointers reduces complexity to O(n²) [OK]
Common Mistakes:
- Thinking brute force is optimal
- Confusing DP with this problem
- Assuming greedy works for closest sum
2. Consider the following Python code implementing the optimal two-pointer forward approach for backspace string compare. What is the value of the variable
write after processing the input string S = "ab#c" inside the build function?easy
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]
Hint: write increments on char, decrements on backspace if possible [OK]
Common Mistakes:
- Counting '#' as a character and incrementing write
- Not decrementing write on backspace
- Off-by-one errors in write pointer
3. Consider the following code snippet for removing duplicates from a sorted array allowing at most two duplicates. What is the value of
write_index after processing the input [1,1,2]?easy
Solution
Step 1: Trace write_index and read_index for input [1,1,2]
Initially, write_index=0. Iteration 0: write_index<2, write 1, write_index=1. Iteration 1: write_index=1<2, write 1, write_index=2. Iteration 2: nums[2]=2 != nums[0]=1, write 2, write_index=3.Step 2: Final write_index value
After processing all elements, write_index=3, indicating the length of the modified array.Final Answer:
Option C -> Option CQuick Check:
All elements allowed, write_index matches array length [OK]
Hint: write_index increments for allowed duplicates [OK]
Common Mistakes:
- Off-by-one errors in write_index
- Confusing read_index with write_index
- Not handling first two elements separately
4. You are given a problem where you must reverse the order of characters in an array in-place, without using extra space. Which algorithmic approach best guarantees an optimal solution in terms of time and space complexity?
easy
Solution
Step 1: Understand the problem constraints
The problem requires reversing characters in-place without extra space, so approaches that use additional arrays are suboptimal.Step 2: Identify the optimal approach
Two pointers starting at opposite ends allow swapping characters in O(n) time and O(1) extra space, fulfilling the problem requirements.Final Answer:
Option A -> Option AQuick Check:
Two-pointer in-place reversal is standard for optimal string reversal [OK]
Hint: Two pointers from ends swap in-place efficiently [OK]
Common Mistakes:
- Thinking DP or greedy applies to reversing strings
- Assuming extra space is allowed
- Confusing brute force with optimal in-place
5. Suppose the 3Sum problem is modified so that elements can be reused multiple times in a triplet (e.g., triplet [0,0,0] is valid if zeros exist). Which modification to the optimal hash set approach correctly handles this variant?
hard
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]
Hint: Allow complement == nums[j] and no skipping duplicates for reuse [OK]
Common Mistakes:
- Removing sorting breaks uniqueness
- Using triple loops is inefficient
- Removing duplicate skipping blindly causes duplicates
