Bird
Raised Fist0
DSA Javaprogramming~20 mins

Two Pointer Technique on Arrays in DSA Java - 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 Java code that uses two pointers to check if any two numbers sum to 10?
DSA Java
public class TwoPointerSum {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 7, 8, 9};
        int left = 0, right = arr.length - 1;
        boolean found = false;
        while (left < right) {
            int sum = arr[left] + arr[right];
            if (sum == 10) {
                found = true;
                break;
            } else if (sum < 10) {
                left++;
            } else {
                right--;
            }
        }
        System.out.println(found);
    }
}
Afalse
BCompilation error
Ctrue
DRuntime exception
Attempts:
2 left
💡 Hint
Think about pairs that add up to 10 in the sorted array.
❓ Predict Output
intermediate
2:00remaining
Resulting Array After Two Pointer Swap
What is the output array after running this Java code that uses two pointers to swap elements?
DSA Java
public class TwoPointerSwap {
    public static void main(String[] args) {
        int[] arr = {5, 3, 8, 6, 2};
        int left = 0, right = arr.length - 1;
        while (left < right) {
            if (arr[left] % 2 == 0) {
                left++;
            } else if (arr[right] % 2 != 0) {
                right--;
            } else {
                int temp = arr[left];
                arr[left] = arr[right];
                arr[right] = temp;
                left++;
                right--;
            }
        }
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}
A2 6 8 3 5
B5 3 8 6 2
C2 3 8 5 6
D6 3 8 2 5
Attempts:
2 left
💡 Hint
The code swaps odd numbers on the left with even numbers on the right.
🧠 Conceptual
advanced
1:30remaining
Two Pointer Technique Use Case
Which of the following problems is best solved using the two pointer technique on arrays?
AReversing a linked list
BFinding the maximum element in an unsorted array
CCounting the frequency of elements in an array
DMerging two sorted arrays into one sorted array
Attempts:
2 left
💡 Hint
Think about how two pointers can traverse two arrays simultaneously.
🔧 Debug
advanced
1:30remaining
Identify the Error in Two Pointer Code
What error will this Java code produce when run?
DSA Java
public class TwoPointerError {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        int left = 0, right = arr.length;
        while (left < right) {
            System.out.print(arr[left] + " ");
            left++;
        }
    }
}
APrints 1 2 3 4 5
BNo output, infinite loop
CCompilation error
DArrayIndexOutOfBoundsException
Attempts:
2 left
💡 Hint
Check the value of right pointer and array indexing.
🚀 Application
expert
3:00remaining
Two Pointer Technique to Find Triplets Summing to Zero
Given a sorted array, which code snippet correctly uses the two pointer technique to find if there exists any triplet that sums to zero?
A
for (int i = 0; i &lt; arr.length; i++) {
    int left = 0, right = arr.length - 1;
    while (left &lt; right) {
        int sum = arr[i] + arr[left] + arr[right];
        if (sum == 0) {
            System.out.println("Found triplet");
            break;
        } else if (sum &lt; 0) {
            right--;
        } else {
            left++;
        }
    }
}
B
for (int i = 0; i &lt; arr.length - 2; i++) {
    int left = i + 1, right = arr.length - 1;
    while (left &lt; right) {
        int sum = arr[i] + arr[left] + arr[right];
        if (sum == 0) {
            System.out.println("Found triplet");
            break;
        } else if (sum &gt; 0) {
            right--;
        } else {
            left++;
        }
    }
}
C
for (int i = 0; i &lt; arr.length - 1; i++) {
    int left = i + 1, right = arr.length - 1;
    while (left &lt;= right) {
        int sum = arr[i] + arr[left] + arr[right];
        if (sum == 0) {
            System.out.println("Found triplet");
            break;
        } else if (sum &gt; 0) {
            right--;
        } else {
            left++;
        }
    }
}
D
for (int i = 0; i &lt; arr.length - 2; i++) {
    int left = i + 1, right = arr.length - 1;
    while (left &lt; right) {
        int sum = arr[i] + arr[left] + arr[right];
        if (sum == 0) {
            System.out.println("Found triplet");
            break;
        } else if (sum &lt; 0) {
            left++;
        } else {
            right--;
        }
    }
}
Attempts:
2 left
💡 Hint
Remember the array is sorted; adjust pointers based on sum comparison.

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
B. Use two indices to traverse the array from different ends or positions simultaneously
C. Use recursion to divide the array into halves
D. Sort the array before processing

Solution

  1. Step 1: Understand the technique's approach

    The two pointer technique uses two indices or pointers to scan the array from different positions, often from start and end.
  2. Step 2: Compare with other options

    Sorting or recursion are different techniques; single pointer is not two pointer technique.
  3. Final Answer:

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

    Two pointers = two indices moving [OK]
Hint: Two pointers move from different sides or positions [OK]
Common Mistakes:
  • Confusing with sorting or recursion
  • Thinking it uses only one pointer
  • Assuming it always requires sorted arrays
2. Which of the following is the correct syntax to initialize two pointers left and right for an array arr in Java?
easy
A. int left = arr.length, right = 0;
B. int left = 1, right = arr.length;
C. int left = 0, right = arr.length - 1;
D. int left = -1, right = arr.length + 1;

Solution

  1. Step 1: Identify valid pointer initialization

    In Java, array indices start at 0 and end at length-1, so left = 0 and right = arr.length - 1 is correct.
  2. Step 2: Check other options for invalid indices

    Options B, C, and D use invalid indices outside array bounds.
  3. Final Answer:

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

    Start at 0 and end at length-1 [OK]
Hint: Array indices start at 0, end at length-1 [OK]
Common Mistakes:
  • Using 1-based indexing
  • Setting pointers outside array bounds
  • Confusing length with last index
3. What will be the output of the following Java code snippet?
int[] arr = {1, 3, 5, 7, 9};
int left = 0, right = arr.length - 1;
int sum = 0;
while (left < right) {
    sum += arr[left] + arr[right];
    left++;
    right--;
}
System.out.println(sum);
medium
A. 15
B. 30
C. 25
D. 20

Solution

  1. Step 1: Trace the loop iterations and sum calculation

    Initial pointers: left=0, right=4 - Iteration 1: sum=0+(1+9)=10, left=1, right=3 - Iteration 2: sum=10+(3+7)=20, left=2, right=2 Loop stops as left is not less than right.
  2. Step 2: Final sum value

    Sum after loop is 20.
  3. Final Answer:

    20 -> Option D
  4. Quick Check:

    Sum pairs (1+9) + (3+7) = 20 [OK]
Hint: Add pairs from ends until pointers meet [OK]
Common Mistakes:
  • Including middle element when left == right
  • Adding elements multiple times
  • Incorrect pointer updates
4. Identify the error in the following Java code using two pointers:
int[] arr = {2, 4, 6, 8};
int left = 0, right = arr.length;
while (left < right) {
    System.out.print(arr[left] + ",");
    left++;
    right--;
}
medium
A. The 'right' pointer is initialized out of array bounds
B. The loop condition should be 'left <= right'
C. The array should be sorted before using two pointers
D. The print statement should be outside the loop

Solution

  1. Step 1: Check pointer initialization

    Array indices go from 0 to length-1, but right is set to arr.length, which is out of bounds.
  2. Step 2: Validate other options

    Loop condition and print placement are valid; sorting is not required here.
  3. Final Answer:

    The 'right' pointer is initialized out of array bounds -> Option A
  4. Quick Check:

    Right pointer must be arr.length - 1 [OK]
Hint: Right pointer must be last valid index (length-1) [OK]
Common Mistakes:
  • Setting right = arr.length instead of arr.length - 1
  • Confusing loop conditions
  • Assuming sorting is always needed
5. Given a sorted array arr and a target sum target, which two-pointer approach correctly finds if there exists a pair of numbers that add up to target?
int left = 0, right = arr.length - 1;
while (left < right) {
    int sum = arr[left] + arr[right];
    if (sum == target) {
        return true;
    } else if (sum < target) {
        left++;
    } else {
        right--;
    }
}
return false;
hard
A. Move left pointer right if sum is less than target, else move right pointer left
B. Always move left pointer right regardless of sum
C. Always move right pointer left regardless of sum
D. Move pointers randomly until sum matches

Solution

  1. Step 1: Understand pointer movement logic

    If sum is less than target, increase sum by moving left pointer right; if sum is more, decrease sum by moving right pointer left.
  2. Step 2: Confirm correctness of approach

    This approach works because array is sorted, so moving pointers adjusts sum towards target.
  3. Final Answer:

    Move left pointer right if sum is less than target, else move right pointer left -> Option A
  4. Quick Check:

    Adjust pointers based on sum vs target [OK]
Hint: Move pointers based on sum comparison with target [OK]
Common Mistakes:
  • Moving pointers without checking sum
  • Ignoring array sorted property
  • Moving both pointers at once
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