Bird
Raised Fist0
Interview Preptwo-pointerseasyFacebookAmazonMicrosoft

Move Zeroes

Choose your preparation mode4 modes available
▶
Steps
setup

Initialize lastNonZeroFoundAt

We start by initializing the pointer 'lastNonZeroFoundAt' to 0, which will track where to place the next non-zero element.

💡 This pointer marks the boundary between processed non-zero elements and the rest of the array.
Line:lastNonZeroFoundAt = 0
💡 Setting this pointer prepares the algorithm to overwrite zero elements with non-zero values as we find them.
📊
Move Zeroes - Watch the Algorithm Execute, Step by Step
Watching each pointer movement and array modification helps you understand how the algorithm partitions the array into processed non-zero elements and remaining elements to be zeroed.
Step 1/14
·Active fill★Answer cell
setup
lastNonZeroFoundAt
0
0
1
1
0
2
3
3
12
4
Result: []
compare
lastNonZeroFoundAt
0
0
1
1
0
2
3
3
12
4
Result: []
compare
lastNonZeroFoundAt
0
0
1
1
0
2
3
3
12
4
Result: []
compare
lastNonZeroFoundAt
0
0
i
1
1
0
2
3
3
12
4
Result: []
swap
1
0
lastNonZeroFoundAt
1
1
0
2
3
3
12
4
Result: [1]
compare
1
0
lastNonZeroFoundAt
1
1
i
0
2
3
3
12
4
Result: [1]
compare
1
0
lastNonZeroFoundAt
1
1
0
2
i
3
3
12
4
Result: [1]
swap
1
0
3
1
lastNonZeroFoundAt
0
2
i
3
3
12
4
Result: [1,3]
compare
1
0
3
1
lastNonZeroFoundAt
0
2
3
3
i
12
4
Result: [1,3]
swap
1
0
3
1
12
2
lastNonZeroFoundAt
3
3
i
12
4
Result: [1,3,12]
move_right
1
0
3
1
12
2
fillIndex
3
3
12
4
Result: [1,3,12]
record
1
0
3
1
12
2
fillIndex
0
3
12
4
Result: [1,3,12]
record
1
0
3
1
12
2
0
3
fillIndex
0
4
Result: [1,3,12]
done
1
0
3
1
12
2
0
3
0
4
Result: [1,3,12]

Key Takeaways

✓ The 'lastNonZeroFoundAt' pointer partitions the array into processed non-zero elements and the rest, enabling in-place compaction.

This insight is hard to see from code alone because the pointer's role is implicit without visualization.

✓ Non-zero elements are overwritten at the front in their original order, preserving relative order while moving zeroes to the end.

Visualizing the overwrite steps clarifies how order is maintained despite in-place modifications.

✓ Zero elements are skipped during traversal and only filled in after all non-zero elements are placed, showing a two-phase approach.

Seeing the separate phases of overwrite and zero fill helps understand why zeroes don't disrupt order.

Practice

(1/5)
1. You are given an integer array and a target value. The task is to count the number of triplets in the array such that the sum of the triplet is less than the target. Which approach guarantees an optimal solution in terms of time complexity?
easy
A. Use three nested loops to check all triplets and count those with sum less than target.
B. Sort the array, then for each element use two pointers to find pairs that form triplets with sum less than target.
C. Use dynamic programming to store counts of sums and build up to triplets.
D. Use a greedy approach by always picking the smallest elements first to form triplets.

Solution

  1. Step 1: Understand problem constraints

    The problem requires counting triplets with sum less than a target, which suggests checking combinations efficiently.
  2. Step 2: Identify approach that reduces complexity

    Sorting and using two pointers allows scanning pairs efficiently for each fixed element, reducing complexity from O(n^3) to O(n^2).
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Sorting + two pointers is a classic pattern for 3Sum variants [OK]
Hint: Sorting + two pointers reduces triple loops to quadratic [OK]
Common Mistakes:
  • Assuming brute force is optimal
  • Thinking DP applies directly here
  • Believing greedy always works for sum constraints
2. You are given an array and a value. You need to remove all occurrences of this value in-place and return the new length of the array after removals. Which approach guarantees an optimal O(n) time and O(1) space solution?
easy
A. Brute force shifting elements left whenever the value is found, resulting in nested loops
B. Two pointers with one scanning from start and the other from end, swapping unwanted elements to the end
C. Sorting the array first, then using binary search to find and remove the value
D. Using a hash set to track elements to keep and reconstruct the array

Solution

  1. Step 1: Understand problem constraints

    The problem requires in-place removal with minimal time and space complexity.
  2. Step 2: Evaluate approaches

    Brute force shifting is O(n^2), sorting changes order and is O(n log n), hash set uses extra space. Two pointers swapping from start and end achieves O(n) time and O(1) space.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Two pointers swapping approach is optimal and in-place [OK]
Hint: Two pointers swapping from ends -> O(n) in-place removal [OK]
Common Mistakes:
  • Confusing sorting or hash sets as optimal in-place solutions
3. 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
A. Using two pointers starting at opposite ends of the array and swapping elements until they meet.
B. Using a brute force approach that creates a new array and copies elements in reverse order.
C. Using a dynamic programming approach to store intermediate reversed substrings.
D. Using a greedy approach that swaps characters only when they are vowels.

Solution

  1. Step 1: Understand the problem constraints

    The problem requires reversing characters in-place without extra space, so approaches that use additional arrays are suboptimal.
  2. 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.
  3. Final Answer:

    Option A -> Option A
  4. Quick 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
4. What is the time complexity of the optimal approach that sorts the array and then uses a sliding window to find the minimum difference between k closest elements?
medium
A. O(n * k) because for each element we check a window of size k
B. O(n^2) because we compare all pairs of elements
C. O(k log n) because we only sort the first k elements
D. O(n log n) due to sorting, plus O(n) for sliding window traversal

Solution

  1. Step 1: Identify sorting complexity

    Sorting the array takes O(n log n) time.
  2. Step 2: Analyze sliding window traversal

    Sliding window moves linearly over the array, O(n) time.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Sorting dominates, sliding window is linear [OK]
Hint: Sorting dominates complexity, sliding window linear [OK]
Common Mistakes:
  • Assuming sliding window is O(n*k) due to window size
  • Confusing sorting only first k elements with full sort
  • Mistaking pairwise comparisons as needed
5. Examine the following buggy code for removing duplicates from a sorted array:
def remove_duplicates(nums):
    if len(nums) == 0:
        return 0
    uniqueIndex = 0
    for currentIndex in range(1, len(nums)):
        if nums[currentIndex] != nums[uniqueIndex]:
            uniqueIndex += 1
            nums[currentIndex] = nums[uniqueIndex]
    return uniqueIndex + 1
Which line contains the bug that causes incorrect behavior?
medium
A. Line 2: if len(nums) == 0: return 0
B. Line 8: nums[currentIndex] = nums[uniqueIndex]
C. Line 6: if nums[currentIndex] != nums[uniqueIndex]:
D. Line 7: uniqueIndex += 1

Solution

  1. Step 1: Identify assignment direction

    The correct code assigns nums[uniqueIndex] = nums[currentIndex] to overwrite duplicates; here it is reversed.
  2. Step 2: Understand impact

    Assigning nums[currentIndex] = nums[uniqueIndex] overwrites scanning pointer's value incorrectly, breaking the unique sequence.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Assignment direction error causes wrong array modification [OK]
Hint: Assignment must overwrite uniqueIndex position, not currentIndex [OK]
Common Mistakes:
  • Swapping assignment indices
  • Forgetting to increment uniqueIndex
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard