an HCL GUVI product

Two Pointers - DSA Interview Questions

The two-pointer technique is an efficient approach used to solve array and string problems by utilizing two indices to traverse data structures. It helps optimize time complexity in problems involving searching, sorting, and partitioning. Mastering this technique is crucial for solving many coding interview problems efficiently.

Practice Two Pointers DSA Coding Problems with Solutions

Learning Objectives:

Learn how to optimize searching, sorting, and partitioning problems using two-pointer strategies. Develop problem-solving skills to handle various array and string-based challenges efficiently.

Exercise Instructions:

  • Start with the first problem and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you with more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Find the Sum of Two Numbers in a Sorted Array

Required Input:

arr = [1, 2, 3, 4, 6], target = 6

Expected Output:

[1, 3]

Code In Python

def two_sum_sorted(arr, target): # Write your logic here pass # Prefilled input arr = [1, 2, 3, 4, 6] target = 6 print(two_sum_sorted(arr, target))

Run Code?

Click Run Button to view compiled output

2. Check if an Array is a Palindrome

Required Input:

arr = [1, 2, 3, 2, 1]

Expected Output:

True

Code In Python

def is_palindrome_array(arr): # Write your logic here pass # Prefilled input arr = [1, 2, 3, 2, 1] print(is_palindrome_array(arr))

Run Code?

Click Run Button to view compiled output

3. Find the Intersection of Two Sorted Arrays

Required Input:

arr1 = [1, 2, 4, 5, 6], arr2 = [2, 4, 6, 8]

Expected Output:

[2, 4, 6]

Code In Python

def find_intersection(arr1, arr2): # Write your logic here pass # Prefilled input arr1 = [1, 2, 4, 5, 6] arr2 = [2, 4, 6, 8] print(find_intersection(arr1, arr2))

Run Code?

Click Run Button to view compiled output

4. Find the Union of Two Sorted Arrays

Required Input:

arr1 = [1, 2, 4, 5, 6], arr2 = [2, 3, 5, 7]

Expected Output:

[1, 2, 3, 4, 5, 6, 7]

Code In Python

def find_union(arr1, arr2): # Write your logic here pass # Prefilled input arr1 = [1, 2, 4, 5, 6] arr2 = [2, 3, 5, 7] print(find_union(arr1, arr2))

Run Code?

Click Run Button to view compiled output

5. Move All Negative Numbers to One Side

Required Input:

arr = [1, -2, 3, -4, 5, -6]

Expected Output:

[-2, -4, -6, 1, 5, 3]

Code In Python

def move_negatives(arr): # Write your logic here pass # Prefilled input arr = [1, -2, 3, -4, 5, -6] print(move_negatives(arr))

Run Code?

Click Run Button to view compiled output

6. Move All Zeros to the End

Required Input:

arr = [0, 1, 0, 3, 12]

Expected Output:

[1, 3, 12, 0, 0]

Code In Python

def move_zeros(arr): # Write your logic here pass # Prefilled input arr = [0, 1, 0, 3, 12] print(move_zeros(arr))

Run Code?

Click Run Button to view compiled output

7. Reverse an Array in Place

Required Input:

arr = [1, 2, 3, 4, 5]

Expected Output:

[5, 4, 3, 2, 1]

Code In Python

def reverse_array(arr): # Write your logic here pass # Prefilled input arr = [1, 2, 3, 4, 5] print(reverse_array(arr))

Run Code?

Click Run Button to view compiled output

8. Find the Pair with Closest Sum to Zero

Required Input:

arr = [-10, -3, 1, 4, 7, 9]

Expected Output:

[-10, 9]

Code In Python

def closest_sum_zero(arr): # Write your logic here pass # Prefilled input arr = [-10, -3, 1, 4, 7, 9] print(closest_sum_zero(arr))

Run Code?

Click Run Button to view compiled output

9. Find the Maximum Water Container

Required Input:

arr = [1, 8, 6, 2, 5, 4, 8, 3, 7]

Expected Output:

49

Code In Python

def max_water_container(arr): # Write your logic here pass # Prefilled input arr = [1, 8, 6, 2, 5, 4, 8, 3, 7] print(max_water_container(arr))

Run Code?

Click Run Button to view compiled output

10. Find the First and Last Position of an Element in a Sorted Array

Required Input:

arr = [5, 7, 7, 8, 8, 10], target = 8

Expected Output:

[3, 4]

Code In Python

def find_first_last_position(arr, target): # Write your logic here pass # Prefilled input arr = [5, 7, 7, 8, 8, 10] target = 8 print(find_first_last_position(arr, target))

Run Code?

Click Run Button to view compiled output

1 of 3