Bird
Raised Fist0
DSA Goprogramming~20 mins

Two Pointer Technique on Arrays in DSA Go - 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 Go code that uses two pointers to check if any two numbers sum to 10?
DSA Go
package main
import "fmt"
func main() {
  arr := []int{1, 2, 4, 4, 5, 6}
  left, right := 0, len(arr)-1
  found := false
  for left < right {
    sum := arr[left] + arr[right]
    if sum == 10 {
      found = true
      break
    } else if sum < 10 {
      left++
    } else {
      right--
    }
  }
  fmt.Println(found)
}
Atrue
Bfalse
CCompilation error
DRuntime panic
Attempts:
2 left
💡 Hint
Think about how the two pointers move to find pairs that sum to 10.
❓ Predict Output
intermediate
2:00remaining
Resulting Array After Removing Duplicates
What is the output of this Go code that uses two pointers to remove duplicates from a sorted array and prints the resulting array slice?
DSA Go
package main
import "fmt"
func main() {
  arr := []int{1, 1, 2, 2, 3, 4, 4, 5}
  if len(arr) == 0 {
    fmt.Println([]int{})
    return
  }
  j := 0
  for i := 1; i < len(arr); i++ {
    if arr[i] != arr[j] {
      j++
      arr[j] = arr[i]
    }
  }
  fmt.Println(arr[:j+1])
}
A[1 2 3 4 5]
B[1 1 2 2 3 4 4 5]
C[1 2 2 3 4 4 5]
D[]
Attempts:
2 left
💡 Hint
The code keeps unique elements at the front and slices the array accordingly.
🔧 Debug
advanced
2:00remaining
Identify the Error in Two Pointer Partition Code
What error does this Go code produce when trying to partition an array around a pivot using two pointers?
DSA Go
package main
import "fmt"
func main() {
  arr := []int{9, 12, 3, 5, 14, 10, 10}
  pivot := 10
  left, right := 0, len(arr)-1
  for left <= right {
    for arr[left] < pivot {
      left++
    }
    for arr[right] > pivot {
      right--
    }
    if left <= right {
      arr[left], arr[right] = arr[right], arr[left]
      left++
      right--
    }
  }
  fmt.Println(arr)
}
A[9 3 5 10 10 14 12]
BInfinite loop
CIndex out of range panic
DCompilation error
Attempts:
2 left
💡 Hint
Check the inner loops and pointer increments carefully.
❓ Predict Output
advanced
2:00remaining
Find Maximum Water Container Area
Given an array representing heights of vertical lines, which option correctly computes the maximum water container area using two pointers?
DSA Go
package main
import "fmt"
func maxArea(height []int) int {
  left, right := 0, len(height)-1
  maxArea := 0
  for left < right {
    width := right - left
    var minHeight int
    if height[left] < height[right] {
      minHeight = height[left]
    } else {
      minHeight = height[right]
    }
    area := width * minHeight
    if area > maxArea {
      maxArea = area
    }
    if height[left] < height[right] {
      left++
    } else {
      right--
    }
  }
  return maxArea
}
func main() {
  fmt.Println(maxArea([]int{1,8,6,2,5,4,8,3,7}))
}
A56
B64
C49
D36
Attempts:
2 left
💡 Hint
The maximum area is between lines at index 1 and 8.
🧠 Conceptual
expert
2:00remaining
Two Pointer Technique Time Complexity
What is the time complexity of the two pointer technique when used to find pairs in a sorted array that sum to a target value?
AO(n log n)
BO(n^2)
CO(log n)
DO(n)
Attempts:
2 left
💡 Hint
Consider how many times each pointer moves.

Practice

(1/5)
1. What is the main idea behind the Two Pointer Technique when used on arrays?
easy
A. Use two arrays to store elements separately.
B. Use one pointer to traverse and another to store values.
C. Use two indices to scan the array from different ends or positions simultaneously.
D. Use two nested loops to compare all pairs of elements.

Solution

  1. Step 1: Understand the technique

    The two pointer technique uses two indices or pointers to traverse the array, often starting from different ends or positions.
  2. Step 2: Compare with other options

    The other options describe different methods, but only using two indices to scan the array from different ends or positions simultaneously correctly describes the two pointer approach.
  3. Final Answer:

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

    Two pointers move independently to solve problems efficiently [OK]
Hint: Two pointers move from ends or positions to reduce time [OK]
Common Mistakes:
  • Thinking it uses two separate arrays
  • Confusing with nested loops approach
  • Assuming only one pointer moves
2. Which of the following is the correct way to initialize two pointers left and right for an array arr in Go?
easy
A. left := 1; right := len(arr)
B. left := 0; right := len(arr) - 1
C. left := -1; right := len(arr)
D. left := 0; right := len(arr)

Solution

  1. Step 1: Understand array indexing in Go

    Arrays and slices in Go are zero-indexed, so the first element is at index 0 and the last at len(arr)-1.
  2. Step 2: Check pointer initialization

    Setting left to 0 and right to len(arr)-1 correctly points to the first and last elements respectively.
  3. Final Answer:

    left := 0; right := len(arr) - 1 -> Option B
  4. Quick Check:

    Start pointers at 0 and last index [OK]
Hint: Start left at 0 and right at last index (len-1) [OK]
Common Mistakes:
  • Setting right to len(arr) causes out-of-range error
  • Starting left at 1 misses first element
  • Using negative indices which Go does not support
3. What will be the output of the following Go code snippet?
arr := []int{1, 3, 5, 7, 9}
left, right := 0, len(arr)-1
for left < right {
    if arr[left]+arr[right] == 10 {
        fmt.Println(left, right)
        break
    } else if arr[left]+arr[right] < 10 {
        left++
    } else {
        right--
    }
}
medium
A. 0 4
B. 1 3
C. 2 3
D. No output

Solution

  1. Step 1: Trace the pointers and sums

    Initial pointers: left=0 (arr[0]=1), right=4 (arr[4]=9), sum=10 ==10, so prints 0 4 and breaks.
  2. Step 2: Verify the condition and loop

    Actually, sum=1+9=10, so it prints 0 4 and breaks immediately.
  3. Final Answer:

    0 4 -> Option A
  4. Quick Check:

    Sum 1+9=10 at indices 0 and 4 [OK]
Hint: Check sum at pointers; print indices when sum matches target [OK]
Common Mistakes:
  • Not breaking after first match
  • Confusing indices with values
  • Incorrect pointer updates
4. Find the error in the following Go code using two pointers to find if any two numbers sum to 8:
arr := []int{2, 4, 6, 8}
left, right := 0, len(arr)
for left < right {
    sum := arr[left] + arr[right]
    if sum == 8 {
        fmt.Println("Found")
        break
    } else if sum < 8 {
        left++
    } else {
        right--
    }
}
medium
A. The loop condition should be left <= right
B. left pointer should start at 1
C. sum calculation is incorrect
D. right is initialized to len(arr), which is out of range

Solution

  1. Step 1: Check pointer initialization

    right is set to len(arr), but valid indices go from 0 to len(arr)-1. This causes out-of-range error when accessing arr[right].
  2. Step 2: Verify other parts

    left starting at 0 is correct, loop condition left < right is valid, sum calculation is correct.
  3. Final Answer:

    right is initialized to len(arr), which is out of range -> Option D
  4. Quick Check:

    Array indices go 0 to len-1, not len [OK]
Hint: Initialize right pointer to len(arr)-1 to avoid out-of-range [OK]
Common Mistakes:
  • Setting right to len(arr) causes runtime error
  • Changing loop condition unnecessarily
  • Misunderstanding array indexing
5. Given a sorted array arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, use the two pointer technique to find all pairs that sum to 10. Which of the following Go code snippets correctly prints all such pairs?
hard
A. left, right := 0, len(arr)-1 for left < right { sum := arr[left] + arr[right] if sum == 10 { fmt.Println(arr[left], arr[right]) left++ right-- } else if sum < 10 { left++ } else { right-- } }
B. left, right := 0, len(arr)-1 for left <= right { sum := arr[left] + arr[right] if sum == 10 { fmt.Println(arr[left], arr[right]) left++ right-- } else if sum < 10 { left++ } else { right-- } }
C. left, right := 0, len(arr) for left < right { sum := arr[left] + arr[right] if sum == 10 { fmt.Println(arr[left], arr[right]) left++ right-- } else if sum < 10 { left++ } else { right-- } }
D. left, right := 1, len(arr)-1 for left < right { sum := arr[left] + arr[right] if sum == 10 { fmt.Println(arr[left], arr[right]) left++ right-- } else if sum < 10 { left++ } else { right-- } }

Solution

  1. Step 1: Check pointer initialization and loop condition

    left, right := 0, len(arr)-1 for left < right { sum := arr[left] + arr[right] if sum == 10 { fmt.Println(arr[left], arr[right]) left++ right-- } else if sum < 10 { left++ } else { right-- } } initializes left=0 and right=len(arr)-1, which are valid indices. Loop runs while left < right to avoid duplicates and out-of-range.
  2. Step 2: Verify pointer updates on sum match

    When sum == 10, both pointers move inward to find other pairs. This correctly finds all pairs without repeats.
  3. Final Answer:

    left, right := 0, len(arr)-1 for left < right { sum := arr[left] + arr[right] if sum == 10 { fmt.Println(arr[left], arr[right]) left++ right-- } else if sum < 10 { left++ } else { right-- } } -> Option A
  4. Quick Check:

    Two pointers move inward, printing pairs when sum matches [OK]
Hint: Move both pointers inward on match to find all pairs [OK]
Common Mistakes:
  • Using right = len(arr) causes index error
  • Loop condition left <= right prints duplicates
  • Starting left at 1 misses pairs with first element
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