Bird
Raised Fist0
DSA C#programming~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 this C# code that uses two pointers to check if any two numbers sum to 10?
DSA C#
int[] arr = {1, 2, 3, 7, 8, 9};
int left = 0, right = arr.Length - 1;
bool found = false;
while (left < right) {
    int sum = arr[left] + arr[right];
    if (sum == 10) {
        found = true;
        break;
    } else if (sum < 10) {
        left++;
    } else {
        right--;
    }
}
Console.WriteLine(found);
ATrue
BFalse
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
Think about pairs from start and end moving inward to find sum 10.
❓ Predict Output
intermediate
2:00remaining
Result of Merging Two Sorted Arrays Using Two Pointers
What is the output list after merging these two sorted arrays using two pointers?
DSA C#
int[] arr1 = {1, 3, 5};
int[] arr2 = {2, 4, 6};
List<int> merged = new List<int>();
int i = 0, j = 0;
while (i < arr1.Length && j < arr2.Length) {
    if (arr1[i] < arr2[j]) {
        merged.Add(arr1[i]);
        i++;
    } else {
        merged.Add(arr2[j]);
        j++;
    }
}
while (i < arr1.Length) {
    merged.Add(arr1[i]);
    i++;
}
while (j < arr2.Length) {
    merged.Add(arr2[j]);
    j++;
}
Console.WriteLine(string.Join(", ", merged));
A1, 2, 4, 3, 5, 6
B1, 3, 5, 2, 4, 6
C2, 4, 6, 1, 3, 5
D1, 2, 3, 4, 5, 6
Attempts:
2 left
💡 Hint
Two pointers pick smaller elements from each array step by step.
🔧 Debug
advanced
2:00remaining
Identify the Error in Two Pointer Palindrome Check
What error does this code produce when checking if a string is a palindrome using two pointers?
DSA C#
string s = "racecar";
int left = 0, right = s.Length - 1;
while (left < right) {
    if (s[left] != s[right]) {
        Console.WriteLine("Not palindrome");
        break;
    }
    left++;
    right--;
}
Console.WriteLine("Palindrome");
ACompilation error due to missing semicolon
BPrints "Not palindrome" twice
CPrints "Palindrome" even if string is not palindrome
DRuntime exception due to index out of range
Attempts:
2 left
💡 Hint
Check what happens after the loop breaks.
🚀 Application
advanced
2:00remaining
Find the Number of Unique Pairs with Given Sum
Given a sorted array, how many unique pairs sum to 7 using two pointers?
DSA C#
int[] arr = {1, 2, 3, 4, 4, 5, 6};
int left = 0, right = arr.Length - 1;
int count = 0;
while (left < right) {
    int sum = arr[left] + arr[right];
    if (sum == 7) {
        count++;
        int leftVal = arr[left];
        int rightVal = arr[right];
        while (left < right && arr[left] == leftVal) left++;
        while (left < right && arr[right] == rightVal) right--;
    } else if (sum < 7) {
        left++;
    } else {
        right--;
    }
}
Console.WriteLine(count);
A3
B4
C2
D5
Attempts:
2 left
💡 Hint
Count pairs (1,6), (2,5), (3,4) ignoring duplicates.
🧠 Conceptual
expert
2:00remaining
Why Two Pointer Technique is Efficient for Sorted Arrays?
Why is the two pointer technique efficient for problems like finding pairs with a target sum in sorted arrays?
AIt sorts the array first to improve performance
BIt reduces time complexity from O(n^2) to O(n) by scanning from both ends simultaneously
CIt uses extra space to store all pairs before checking sums
DIt uses recursion to divide the array into smaller parts
Attempts:
2 left
💡 Hint
Think about how scanning from both ends avoids nested loops.

Practice

(1/5)
1. What is the main idea behind the Two Pointer Technique when used on arrays?
easy
A. Use two indices to traverse the array from different ends or positions simultaneously
B. Use one pointer to traverse and another to store values
C. Sort the array first and then use binary search
D. Use recursion to split the array into two parts

Solution

  1. Step 1: Understand the two pointer approach

    The technique uses two pointers (indices) to scan the array from different positions, often from start and end. This helps solve problems efficiently by reducing the number of iterations compared to nested loops.
  2. Final Answer:

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

    Two pointers move from different ends = D [OK]
Hint: Two pointers move from start and end to meet in middle [OK]
Common Mistakes:
  • Thinking it always requires sorting first
  • Using only one pointer instead of two
  • Confusing with recursion or binary search
2. Which of the following is the correct syntax to initialize two pointers left and right for an array arr in C#?
easy
A. int left = -1, right = arr.Length + 1;
B. int left = 1, right = arr.Length;
C. int left = arr.Length, right = 0;
D. int left = 0, right = arr.Length - 1;

Solution

  1. Step 1: Understand array indexing and initialize pointers correctly

    Arrays start at index 0 and end at index Length - 1. Left pointer starts at 0 (first element), right pointer at Length - 1 (last element).
  2. Final Answer:

    int left = 0, right = arr.Length - 1; -> Option D
  3. Quick Check:

    Start at 0 and end at Length-1 = A [OK]
Hint: Start left at 0, right at length minus one [OK]
Common Mistakes:
  • Starting right pointer at arr.Length (out of bounds)
  • Starting left pointer at 1 instead of 0
  • Using negative indices
3. What will be the output of the following C# code?
int[] arr = {1, 2, 3, 4, 5, 6};
int left = 0, right = arr.Length - 1;
int sum = 0;
while (left < right) {
    sum += arr[left] + arr[right];
    left++;
    right--;
}
Console.WriteLine(sum);
medium
A. 21
B. 18
C. 20
D. 15

Solution

  1. Step 1: Trace the loop iterations

    Pairs added: (1+6)=7, (2+5)=7, (3+4)=7; sum = 7+7+7 = 21. Loop runs while left < right, so stops after these pairs.
  2. Final Answer:

    21 -> Option A
  3. Quick Check:

    Sum of pairs from ends = 21 [OK]
Hint: Sum pairs from ends until pointers meet [OK]
Common Mistakes:
  • Adding elements twice
  • Loop running when left equals right
  • Incorrect sum calculation
4. Identify the error in the following C# code using two pointers to find if array arr contains two numbers that sum 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;
medium
A. The sum calculation is incorrect
B. The pointers should both start at 0
C. The loop condition should be left < right instead of left <= right
D. The return statements are missing a value

Solution

  1. Step 1: Analyze loop condition

    Using left <= right allows pointers to be equal, causing sum of same element twice. Change to left < right to avoid using the same element twice.
  2. Final Answer:

    The loop condition should be left < right instead of left <= right -> Option C
  3. Quick Check:

    Pointers must not cross or equal for two distinct elements [OK]
Hint: Use left < right to avoid same element twice [OK]
Common Mistakes:
  • Allowing pointers to be equal causing wrong sums
  • Starting both pointers at 0
  • Misplacing return statements
5. Given a sorted array arr and a number k, how can you use the two pointer technique to find if there exists a pair with sum equal to k? Choose the correct approach.
hard
A. Initialize both pointers at start; move both right until sum equals k
B. Initialize two pointers at start and end; move left pointer right if sum < k, else move right pointer left
C. Initialize two pointers at middle; move both outward until sum equals k
D. Sort the array again and use binary search for each element

Solution

  1. Step 1: Set pointers and move based on sum

    Left pointer at 0, right pointer at last index. If sum < k, move left right to increase sum; if sum > k, move right left to decrease sum.
  2. Final Answer:

    Initialize two pointers at start and end; move left pointer right if sum < k, else move right pointer left -> Option B
  3. Quick Check:

    Two pointers adjust to reach target sum efficiently [OK]
Hint: Move pointers inward based on sum comparison [OK]
Common Mistakes:
  • Starting both pointers at same side
  • Moving pointers in wrong direction
  • Sorting array again unnecessarily
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