Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Two Pointer Technique on Arrays in DSA Typescript - 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 this TypeScript code that uses two pointers to find if a pair sums to 9?
DSA Typescript
function hasPairWithSum(arr: number[], target: number): boolean {
  let left = 0;
  let 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;
}

const result = hasPairWithSum([1, 2, 4, 4, 5, 6, 8], 9);
console.log(result);
Afalse
Bundefined
Ctrue
DTypeError
Attempts:
2 left
💡 Hint
Think about pairs that add up to 9 in the sorted array.
❓ Predict Output
intermediate
2:00remaining
Result of Merging Two Sorted Arrays Using Two Pointers
What is the output of this TypeScript code that merges two sorted arrays using two pointers?
DSA Typescript
function mergeSortedArrays(arr1: number[], arr2: number[]): number[] {
  let i = 0, j = 0;
  const merged: number[] = [];
  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++;
  }
  return merged;
}

const result = mergeSortedArrays([1, 3, 5], [2, 4, 6]);
console.log(result);
A[1, 3, 5, 2, 4, 6]
B[1, 2, 3, 5, 4, 6]
C[2, 4, 6, 1, 3, 5]
D[1, 2, 3, 4, 5, 6]
Attempts:
2 left
💡 Hint
The function merges two sorted arrays into one sorted array.
🔧 Debug
advanced
2:00remaining
Identify the Error in Two Pointer Palindrome Check
What error will this TypeScript code produce when checking if a string is a palindrome using two pointers?
DSA Typescript
function isPalindrome(s: string): boolean {
  let left = 0;
  let right = s.length - 1;
  while (left <= right) {
    if (s[left] !== s[right]) {
      return false;
    }
    left++;
    right--;
  }
  return true;
}

console.log(isPalindrome('racecar'));
ANo error, outputs true
BTypeError: s[left] is undefined
CSyntaxError
DInfinite loop
Attempts:
2 left
💡 Hint
Check the loop condition and indexing carefully.
❓ Predict Output
advanced
2:00remaining
Output of Removing Duplicates from Sorted Array Using Two Pointers
What is the output of this TypeScript code that removes duplicates from a sorted array in-place using two pointers?
DSA Typescript
function removeDuplicates(nums: number[]): number {
  if (nums.length === 0) return 0;
  let i = 0;
  for (let j = 1; j < nums.length; j++) {
    if (nums[j] !== nums[i]) {
      i++;
      nums[i] = nums[j];
    }
  }
  return i + 1;
}

const nums = [1, 1, 2, 2, 3, 4, 4];
const length = removeDuplicates(nums);
console.log(length);
console.log(nums.slice(0, length));
A
4
[1, 2, 3, 4]
B
5
[1, 2, 3, 4, 4]
C
7
[1, 1, 2, 2, 3, 4, 4]
D
3
[1, 2, 3]
Attempts:
2 left
💡 Hint
The function returns the count of unique elements and modifies the array in-place.
🧠 Conceptual
expert
2:00remaining
Two Pointer Technique Application Scenario
Which of the following problems is best solved using the two pointer technique on arrays?
AFinding the maximum subarray sum in an unsorted array
BChecking if a sorted array contains two numbers that sum to a target
CSorting an array using quicksort
DFinding the shortest path in a graph
Attempts:
2 left
💡 Hint
Two pointers work well when the array is sorted and you need to find pairs.

Practice

(1/5)
1. What is the main idea behind the Two Pointer Technique when used on arrays?
easy
A. Use a single pointer to iterate through the array once
B. Use two indices to scan the array from different ends or positions simultaneously
C. Sort the array before processing it with one pointer
D. Use recursion to divide the array into two parts

Solution

  1. Step 1: Understand the technique

    The Two Pointer Technique involves using two indices or pointers to traverse the array, often from opposite ends or different positions.
  2. Step 2: Compare with other options

    Options A, C, and D describe different methods like single pointer iteration, sorting, or recursion, which are not the core idea of two pointers.
  3. Final Answer:

    Use two indices to scan the array from different ends or positions simultaneously -> Option B
  4. Quick Check:

    Two pointers = two indices scanning array [OK]
Hint: Two pointers move independently to cover array efficiently [OK]
Common Mistakes:
  • Thinking it uses only one pointer
  • Confusing with sorting or recursion
  • Assuming pointers move only forward
2. Which of the following is the correct TypeScript syntax to initialize two pointers at the start and end of an array named arr?
easy
A. let left = arr[0], right = arr[arr.length];
B. let left = 1, right = arr.length;
C. let left = 0, right = arr.length - 1;
D. let left = arr.length, right = 0;

Solution

  1. Step 1: Identify correct pointer initialization

    For two pointers at array ends, left starts at 0 (first index), right at arr.length - 1 (last index).
  2. Step 2: Check other options for errors

    let left = 1, right = arr.length; uses invalid indices (arr.length is out of bounds). let left = arr[0], right = arr[arr.length]; assigns values from array elements, not indices. let left = arr.length, right = 0; reverses pointers incorrectly.
  3. Final Answer:

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

    Start = 0, End = length-1 [OK]
Hint: Array indices start at 0, last index is length minus one [OK]
Common Mistakes:
  • Using arr.length as an index (out of bounds)
  • Assigning pointer to array values instead of indices
  • Swapping left and right initialization
3. What will be the output of the following TypeScript code?
const arr = [1, 3, 5, 7, 9];
let left = 0;
let right = arr.length - 1;
let sumPairs = [];
while (left < right) {
  sumPairs.push(arr[left] + arr[right]);
  left++;
  right--;
}
console.log(sumPairs);
medium
A. [10, 16]
B. [10, 12]
C. [10, 8]
D. [10, 10]

Solution

  1. Step 1: Trace the loop iterations

    Initial pointers: left=0, right=4
    Iteration 1: arr[0]+arr[4] = 1+9=10, sumPairs=[10]
    left=1, right=3
    Iteration 2: arr[1]+arr[3] = 3+7=10, sumPairs=[10,10]
    left=2, right=2 (loop ends as left is not less than right)
  2. Step 2: Final output array

    The sumPairs array contains [10, 10] after the loop finishes.
  3. Final Answer:

    [10, 10] -> Option D
  4. Quick Check:

    Pairs sums = [10, 10] [OK]
Hint: Add elements at left and right, move pointers inward [OK]
Common Mistakes:
  • Including middle element sum when left equals right
  • Incorrect pointer updates causing infinite loop
  • Wrong sum calculations or indices
4. Identify the bug in the following TypeScript code that uses two pointers to find if a sorted array contains two numbers summing to target.
function hasPairSum(arr: number[], target: number): boolean {
  let left = 0;
  let right = arr.length;
  while (left < right) {
    const sum = arr[left] + arr[right];
    if (sum === target) return true;
    else if (sum < target) left++;
    else right--;
  }
  return false;
}
medium
A. Right pointer initialized to arr.length instead of arr.length - 1
B. Left pointer should start at 1, not 0
C. The loop condition should be left <= right
D. The sum calculation should use arr[left] - arr[right]

Solution

  1. Step 1: Check pointer initialization

    The right pointer is set to arr.length, which is out of array bounds. It should be arr.length - 1 to point to the last element.
  2. Step 2: Validate other parts

    Left pointer at 0 is correct. Loop condition left < right is standard. Sum calculation uses addition correctly.
  3. Final Answer:

    Right pointer initialized to arr.length instead of arr.length - 1 -> Option A
  4. Quick Check:

    Right pointer must be last index (length-1) [OK]
Hint: Array last index is length minus one, not length [OK]
Common Mistakes:
  • Using arr.length as index causing runtime error
  • Changing loop condition incorrectly
  • Mistaking sum operation for subtraction
5. Given a sorted array arr of integers, how can you use the Two Pointer Technique to find all unique pairs that sum up to a target value target? Choose the best approach.
hard
A. Initialize two pointers at start and end; move inward adjusting pointers based on sum; skip duplicates to collect unique pairs
B. Use nested loops to check all pairs and add those matching target
C. Sort the array first, then use a hash map to find complements
D. Initialize pointers both at start and move forward checking sums

Solution

  1. Step 1: Use two pointers at array ends

    Start with left at 0 and right at arr.length - 1. Calculate sum of elements at these pointers.
  2. Step 2: Move pointers and skip duplicates

    If sum equals target, record pair and move both pointers skipping duplicates. If sum less than target, move left pointer right. If sum greater, move right pointer left.
  3. Step 3: Collect unique pairs efficiently

    This approach avoids nested loops and handles duplicates by skipping repeated values, ensuring unique pairs.
  4. Final Answer:

    Initialize two pointers at start and end; move inward adjusting pointers based on sum; skip duplicates to collect unique pairs -> Option A
  5. Quick Check:

    Two pointers + skip duplicates = unique pairs [OK]
Hint: Move pointers inward and skip duplicates for unique pairs [OK]
Common Mistakes:
  • Using nested loops causing higher time complexity
  • Not skipping duplicates leading to repeated pairs
  • Incorrect pointer movement causing missed pairs
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