Bird
0
0
DSA Cprogramming~20 mins

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

Choose your learning style9 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.