Bird
Raised Fist0
DSA Cprogramming~20 mins

Two Pointer Technique on Arrays in DSA C - 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 C code that uses two pointers to check if any two numbers in a sorted array sum to a target value?
DSA C
#include <stdio.h>

int main() {
    int arr[] = {1, 3, 4, 5, 7, 10, 12};
    int target = 9;
    int left = 0, right = 6;
    int found = 0;

    while (left < right) {
        int sum = arr[left] + arr[right];
        if (sum == target) {
            found = 1;
            break;
        } else if (sum < target) {
            left++;
        } else {
            right--;
        }
    }

    if (found) {
        printf("Found: %d + %d = %d\n", arr[left], arr[right], target);
    } else {
        printf("Not Found\n");
    }
    return 0;
}
AFound: 4 + 5 = 9
BFound: 3 + 7 = 9
CNot Found
DFound: 1 + 12 = 9
Attempts:
2 left
💡 Hint
Think about how the two pointers move based on the sum compared to the target.
🧠 Conceptual
intermediate
1:30remaining
Two Pointer Technique Purpose
What is the main advantage of using the two pointer technique on a sorted array compared to a nested loop approach?
AIt uses less memory by storing fewer elements
BIt reduces time complexity from O(n^2) to O(n)
CIt sorts the array automatically
DIt allows searching for multiple targets simultaneously
Attempts:
2 left
💡 Hint
Think about how many times each element is visited in two pointer vs nested loops.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Two Pointer Implementation
What error will the following code produce when trying to find if two numbers sum to target in a sorted array?
DSA C
#include <stdio.h>

int main() {
    int arr[] = {2, 4, 6, 8, 10};
    int target = 14;
    int left = 0, right = 4;

    while (left <= right) {
        int sum = arr[left] + arr[right];
        if (sum == target) {
            printf("Found: %d + %d = %d\n", arr[left], arr[right], target);
            return 0;
        } else if (sum < target) {
            left++;
        } else {
            right--;
        }
    }
    printf("Not Found\n");
    return 0;
}
AInfinite loop because left can become equal to right and pointers do not move
BRuns correctly and prints Not Found
CRuns correctly and prints Found: 4 + 10 = 14
DRuns correctly but prints Found: 6 + 8 = 14
Attempts:
2 left
💡 Hint
Check the loop condition and pointer movements carefully.
❓ Predict Output
advanced
2:00remaining
Output of Two Pointer Removing Duplicates
What is the output of this C code that removes duplicates from a sorted array using two pointers?
DSA C
#include <stdio.h>

int main() {
    int arr[] = {1, 1, 2, 2, 3, 4, 4, 5};
    int n = 8;
    int i = 0, j = 1;

    while (j < n) {
        if (arr[i] != arr[j]) {
            i++;
            arr[i] = arr[j];
        }
        j++;
    }

    printf("Array after removing duplicates: ");
    for (int k = 0; k <= i; k++) {
        printf("%d ", arr[k]);
    }
    printf("\n");
    return 0;
}
AArray after removing duplicates: 1 2 2 3 4 5
BArray after removing duplicates: 1 1 2 3 4 5
CArray after removing duplicates: 1 2 3 4 4 5
DArray after removing duplicates: 1 2 3 4 5
Attempts:
2 left
💡 Hint
Check how the pointers move and how duplicates are skipped.
🧠 Conceptual
expert
1:30remaining
Two Pointer Technique Limitations
Which of the following is a limitation of the two pointer technique on arrays?
AIt requires the array to be sorted or have a specific order
BIt cannot be used to find pairs with a given sum
CIt always uses extra memory proportional to array size
DIt only works on arrays with even number of elements
Attempts:
2 left
💡 Hint
Think about the assumptions needed for two pointers to work correctly.

Practice

(1/5)
1. What is the main idea behind the Two Pointer Technique when used on arrays?
easy
A. Use one pointer to traverse and another to store the sum.
B. Use two arrays to store elements separately.
C. Use two indices to traverse the array from different ends or positions simultaneously.
D. Use two pointers to swap elements randomly.

Solution

  1. Step 1: Understand the technique

    The two pointer technique involves using two indices or pointers to scan the array, often from the start and end, to solve problems efficiently.
  2. Step 2: Identify the correct description

    Use two indices to traverse the array from different ends or positions simultaneously. correctly describes using two pointers to traverse the array from different ends or positions simultaneously.
  3. Final Answer:

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

    Two pointers move from different positions = A [OK]
Hint: Two pointers move from different ends or positions [OK]
Common Mistakes:
  • Thinking two pointers mean two separate arrays
  • Confusing pointers with storing sums
  • Assuming pointers swap elements randomly
2. Which of the following is the correct syntax to initialize two pointers left and right for an array arr of size n in C?
easy
A. int left = 0, right = n - 1;
B. int left = 1, right = n;
C. int left = n, right = 0;
D. int left = n - 1, right = 0;

Solution

  1. Step 1: Understand pointer initialization

    For two pointers starting at the beginning and end of an array, left should be 0 and right should be n - 1 because array indices start at 0.
  2. Step 2: Check options

    int left = 0, right = n - 1; correctly initializes left = 0 and right = n - 1. Other options have incorrect indices.
  3. Final Answer:

    int left = 0, right = n - 1; -> Option A
  4. Quick Check:

    Start at 0 and end at n-1 = C [OK]
Hint: Array indices start at 0, so right = n - 1 [OK]
Common Mistakes:
  • Starting left at 1 instead of 0
  • Setting right to n (out of bounds)
  • Swapping left and right initialization
3. What will be the output of the following C code snippet?
int arr[] = {1, 3, 5, 7, 9};
int left = 0, right = 4;
int sum = 0;
while (left < right) {
    sum += arr[left] + arr[right];
    left++;
    right--;
}
printf("%d", sum);
medium
A. 25
B. 15
C. 30
D. 20

Solution

  1. Step 1: Trace the loop iterations

    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 ends because left is not less than right.
  2. Step 2: Calculate final sum

    Sum after loop = 20. Note that the middle element (index 2) is not added because left == right stops the loop.
  3. Final Answer:

    20 -> Option D
  4. Quick Check:

    Sum pairs (1+9)+(3+7) = 20 [OK]
Hint: Sum pairs while left < right, middle element skipped [OK]
Common Mistakes:
  • Including middle element when left == right
  • Adding elements twice
  • Incorrect loop condition
4. Identify the error in the following code snippet that uses the two pointer technique to find if a sorted array contains two numbers that sum to a target value.
int arr[] = {2, 4, 6, 8, 10};
int left = 0, right = 4;
int target = 14;
while (left <= right) {
    int sum = arr[left] + arr[right];
    if (sum == target) {
        printf("Found");
        break;
    } else if (sum < target) {
        left++;
    } else {
        right--;
    }
}
medium
A. The pointers left and right are initialized incorrectly.
B. The loop condition should be left < right, not left <= right.
C. The sum comparison logic is reversed.
D. The printf statement should be outside the loop.

Solution

  1. Step 1: Analyze loop condition

    The loop uses left <= right, which allows pointers to meet or cross. This can cause incorrect checks or double counting.
  2. Step 2: Correct loop condition

    Using left < right ensures pointers do not cross and only valid pairs are checked.
  3. Final Answer:

    The loop condition should be left < right, not left <= right. -> Option B
  4. Quick Check:

    Pointers must not cross: left < right [OK]
Hint: Use left < right to avoid pointer crossing [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Swapping pointer increment/decrement logic
  • Incorrect pointer initialization
5. Given a sorted array arr of size n, how can you use the two pointer technique to find all unique pairs that sum to a target value t? Choose the best approach.
hard
A. Initialize two pointers at start and end; move pointers inward based on sum comparison; skip duplicates to find unique pairs.
B. Use nested loops to check all pairs and store sums in a hash set.
C. Sort the array and use binary search for each element to find complement.
D. Initialize both pointers at start and move both forward to find pairs.

Solution

  1. Step 1: Use two pointers at array ends

    Start with left = 0 and right = n - 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 < target, move left pointer right. If sum > target, move right pointer left.
  3. Final Answer:

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

    Two pointers from ends + skip duplicates = D [OK]
Hint: Move pointers inward and skip duplicates for unique pairs [OK]
Common Mistakes:
  • Using nested loops instead of two pointers
  • Not skipping duplicates causing repeated pairs
  • Moving both pointers forward instead of inward
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