Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Two Pointer Technique on Arrays in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Two Pointer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Two Pointer Sum Check
What is the output of the following code that uses two pointers to check if any two numbers sum to 10?
DSA Javascript
const arr = [1, 2, 3, 7, 8, 9];
let left = 0;
let right = arr.length - 1;
let found = false;
while (left < right) {
  const sum = arr[left] + arr[right];
  if (sum === 10) {
    found = true;
    break;
  } else if (sum < 10) {
    left++;
  } else {
    right--;
  }
}
console.log(found);
ATypeError
Bundefined
Ctrue
Dfalse
Attempts:
2 left
💡 Hint
Think about pairs that add up to 10 in the sorted array.
❓ Predict Output
intermediate
2:00remaining
Resulting Array After Removing Duplicates Using Two Pointers
What is the output array after running this two pointer code that removes duplicates from a sorted array?
DSA Javascript
const nums = [1, 1, 2, 2, 3, 4, 4];
let i = 0;
for (let j = 1; j < nums.length; j++) {
  if (nums[j] !== nums[i]) {
    i++;
    nums[i] = nums[j];
  }
}
const result = nums.slice(0, i + 1);
console.log(result);
A[1, 2, 3, 4]
B[1, 1, 2, 3, 4]
C[1, 2, 2, 3, 4]
D[1, 2, 3, 4, 4]
Attempts:
2 left
💡 Hint
The code keeps unique elements at the start of the array.
❓ Predict Output
advanced
2:00remaining
Output of Merging Two Sorted Arrays Using Two Pointers
What is the output of merging two sorted arrays using two pointers?
DSA Javascript
const arr1 = [1, 3, 5];
const arr2 = [2, 4, 6];
const merged = [];
let i = 0, j = 0;
while (i < arr1.length && j < arr2.length) {
  if (arr1[i] < arr2[j]) {
    merged.push(arr1[i]);
    i++;
  } else {
    merged.push(arr2[j]);
    j++;
  }
}
while (i < arr1.length) {
  merged.push(arr1[i]);
  i++;
}
while (j < arr2.length) {
  merged.push(arr2[j]);
  j++;
}
console.log(merged);
A[1, 2, 3, 4, 5, 6]
B[1, 3, 5, 2, 4, 6]
C[2, 4, 6, 1, 3, 5]
D[1, 2, 4, 3, 5, 6]
Attempts:
2 left
💡 Hint
Two pointers pick the smaller element from each array step by step.
❓ Predict Output
advanced
2:00remaining
Output of Two Pointer Technique to Reverse a String Array
What is the output after reversing the array using two pointers?
DSA Javascript
const arr = ['a', 'b', 'c', 'd', 'e'];
let left = 0;
let right = arr.length - 1;
while (left < right) {
  const temp = arr[left];
  arr[left] = arr[right];
  arr[right] = temp;
  left++;
  right--;
}
console.log(arr);
A['a', 'e', 'c', 'd', 'b']
B['a', 'b', 'c', 'd', 'e']
C['e', 'b', 'c', 'd', 'a']
D['e', 'd', 'c', 'b', 'a']
Attempts:
2 left
💡 Hint
Two pointers swap elements from start and end moving inward.
🧠 Conceptual
expert
3:00remaining
Two Pointer Technique for Finding All Unique Triplets Summing to Zero
Which of the following best describes the two pointer approach to find all unique triplets in an array that sum to zero?
AUse three nested loops to check all triplets without sorting the array.
BSort the array, fix one element, then use two pointers to find pairs that sum to the negative of the fixed element, skipping duplicates.
CSort the array and use a hash set to store all pairs, then check for the third element.
DUse a single pointer to iterate and check if the sum of the next two elements is zero.
Attempts:
2 left
💡 Hint
Think about how sorting helps and how two pointers can find pairs efficiently.

Practice

(1/5)
1. What is the main idea behind the Two Pointer Technique when used on arrays?
easy
A. Use two indices moving towards each other or in the same direction to solve problems efficiently
B. Use two nested loops to compare every pair of elements
C. Sort the array first and then use binary search
D. Use recursion to divide the array into two parts

Solution

  1. Step 1: Understand the technique

    The two pointer technique uses two indices (pointers) to traverse the array, often starting from opposite ends or the same end.
  2. Step 2: Purpose of two pointers

    This helps solve problems like finding pairs, removing duplicates, or partitioning without extra space and in linear time.
  3. Final Answer:

    Use two indices moving towards each other or in the same direction to solve problems efficiently -> Option A
  4. Quick Check:

    Two pointers = two indices moving efficiently [OK]
Hint: Think of two fingers scanning array ends or start [OK]
Common Mistakes:
  • Confusing with nested loops approach
  • Assuming sorting is always required
  • Thinking recursion is part of two pointers
2. Which of the following is the correct syntax to initialize two pointers left and right for an array arr in JavaScript?
easy
A. var left = 1, right = arr.length;
B. let left = arr.length, right = 0;
C. const left = arr.length - 1, right = 0;
D. const left = 0, right = arr.length - 1;

Solution

  1. Step 1: Understand pointer initialization

    For two pointers starting at the array ends, left starts at 0 (start), right at arr.length - 1 (end).
  2. Step 2: Check each option

    const left = 0, right = arr.length - 1; correctly sets left = 0 and right = arr.length - 1. Others have incorrect indices.
  3. Final Answer:

    const left = 0, right = arr.length - 1; -> Option D
  4. Quick Check:

    Start pointer at 0, end pointer at length-1 [OK]
Hint: Start left at 0 and right at last index [OK]
Common Mistakes:
  • Setting right pointer to arr.length (out of bounds)
  • Swapping left and right initialization
  • Using 1-based indexing instead of 0-based
3. What will be the output of the following JavaScript code?
const arr = [1, 2, 3, 4, 5];
let left = 0;
let right = arr.length - 1;
const result = [];
while (left <= right) {
  if (arr[left] + arr[right] === 6) {
    result.push([arr[left], arr[right]]);
    left++;
    right--;
  } else if (arr[left] + arr[right] < 6) {
    left++;
  } else {
    right--;
  }
}
console.log(result);
medium
A. [[2,4],[3,3]]
B. [[1,5],[3,3]]
C. [[1,5],[2,4],[3,3]]
D. [[1,5],[2,4]]

Solution

  1. Step 1: Trace the two pointers and sums

    Initially left=0 (1), right=4 (5), sum=6 -> push [1,5], move left=1, right=3.
    Now left=1 (2), right=3 (4), sum=6 -> push [2,4], move left=2, right=2.
    Now left=2 (3), right=2 (3), sum=6 -> push [3,3], move left=3, right=1 but loop ends since left > right.
  2. Step 2: Check the result array

    Result contains [[1,5],[2,4],[3,3]]. But note the condition left <= right allows the middle element pairing only if left equals right.
    Here, at left=2 and right=2, sum=6, so [3,3] is added.
  3. Final Answer:

    [[1,5],[2,4],[3,3]] -> Option C
  4. Quick Check:

    Pairs summing to 6 = [[1,5],[2,4],[3,3]] [OK]
Hint: Check sums at left and right, move pointers accordingly [OK]
Common Mistakes:
  • Ignoring the case when left equals right
  • Not updating pointers correctly after finding a pair
  • Assuming only pairs with different indices count
4. Find the bug in the following code that uses two pointers to check if an array is sorted in non-decreasing order.
function isSorted(arr) {
  let left = 0;
  let right = arr.length - 1;
  while (left < right) {
    if (arr[left] > arr[left + 1]) {
      return false;
    }
    left++;
  }
  return true;
}
medium
A. The loop condition should be left <= right
B. The right pointer is unused and unnecessary
C. The comparison should be between arr[left] and arr[right]
D. The function does not handle empty arrays

Solution

  1. Step 1: Analyze pointer usage

    The variable right is set but never used inside the loop or condition, making it unnecessary.
  2. Step 2: Check loop and comparison logic

    The loop correctly checks adjacent elements using left pointer only, so right can be removed without affecting logic.
  3. Final Answer:

    The right pointer is unused and unnecessary -> Option B
  4. Quick Check:

    Unused variable right pointer = bug [OK]
Hint: Check if all declared variables are used [OK]
Common Mistakes:
  • Thinking right pointer must be used in two pointer technique always
  • Changing loop condition unnecessarily
  • Confusing adjacent element checks with two pointer sum checks
5. Given a sorted array arr and a target sum target, which code snippet correctly uses the two pointer technique to find if there exists a pair that sums to target?
const arr = [1, 3, 4, 5, 7, 10, 12];
const target = 9;
// Which snippet is correct?
hard
A. let left = 0, right = arr.length - 1; while (left < right) { const sum = arr[left] + arr[right]; if (sum === target) return true; else if (sum < target) left++; else right--; } return false;
B. let left = 0, right = arr.length - 1; while (left <= right) { const sum = arr[left] + arr[right]; if (sum === target) return true; else if (sum > target) left++; else right--; } return false;
C. for (let left = 0, right = arr.length - 1; left < right; left++, right--) { if (arr[left] + arr[right] === target) return true; } return false;
D. let left = 0, right = 0; while (left < arr.length && right < arr.length) { if (arr[left] + arr[right] === target) return true; else if (arr[left] + arr[right] < target) right++; else left++; } return false;

Solution

  1. Step 1: Understand two pointer approach for pair sum

    Start with pointers at array ends: left=0, right=length-1. Move pointers based on sum compared to target.
  2. Step 2: Check each option logic

    let left = 0, right = arr.length - 1; while (left < right) { const sum = arr[left] + arr[right]; if (sum === target) return true; else if (sum < target) left++; else right--; } return false; correctly moves left pointer forward if sum < target, right pointer backward if sum > target, and returns true if sum equals target.
    let left = 0, right = arr.length - 1; while (left <= right) { const sum = arr[left] + arr[right]; if (sum === target) return true; else if (sum > target) left++; else right--; } return false; incorrectly moves left pointer forward when sum > target.
    for (let left = 0, right = arr.length - 1; left < right; left++, right--) { if (arr[left] + arr[right] === target) return true; } return false; moves both pointers every iteration, missing some pairs.
    let left = 0, right = 0; while (left < arr.length && right < arr.length) { if (arr[left] + arr[right] === target) return true; else if (arr[left] + arr[right] < target) right++; else left++; } return false; starts both pointers at 0, which is incorrect for this problem.
  3. Final Answer:

    Option A code snippet -> Option A
  4. Quick Check:

    Correct pointer moves based on sum vs target [OK]
Hint: Move left if sum less, right if sum more [OK]
Common Mistakes:
  • Moving wrong pointer when sum is greater or smaller
  • Using for loop that moves both pointers simultaneously
  • Starting both pointers at same index
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