0% found this document useful (0 votes)
9 views

LeetCode Opposite Pointer Practice Questions

Uploaded by

odeinanyanwu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

LeetCode Opposite Pointer Practice Questions

Uploaded by

odeinanyanwu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Here’s a set of practice questions to help you test and improve your

understanding of two-pointer techniques. They progress from beginner to


expert level, with step-by-step explanations of what each question is asking
you to do.

Beginner Level
Find a pair with a specific sum in a sorted array
Write a function to find any two numbers in a sorted list that add up to a
given target.
Example:

Input: arr = [1, 2, 3, 4, 5], target = 6


Output: [1, 5] or [2, 4]

1. Test yourself:

○ What happens if no pair adds up to the target?


○ What if the array is empty?

Check if an array is a palindrome


Write a function to check if a list reads the same backward as forward. Use
two pointers to compare the numbers at the start and end of the list.
Example:

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


Output: True

2. Test yourself:

○ What should the function return for [1, 2, 3, 4, 5]?


○ What about a single number, like [1]?
Merge two sorted arrays
Merge two sorted arrays into one sorted array using two pointers.
Example:

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


Output: [1, 2, 3, 4, 5, 6]

3. Test yourself:

○ How do you handle arrays of different lengths?


○ What if one array is empty?

Intermediate Level
Move zeros to the end
Write a function to move all the zeros in a list to the end while keeping the
other numbers in the same order.
Example:

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


Output: [1, 3, 12, 0, 0]

4. Test yourself:

○ What happens if there are no zeros?


○ Can you do it without creating a new list?

Find the closest pair


Given a sorted list and a target number, find the two numbers whose sum
is closest to the target.
Example:

Input: arr = [1, 3, 4, 7, 10], target = 15


Output: [4, 10]

5. Test yourself:

○ What happens if the target is smaller than the smallest pair in


the list?
○ What if the array contains duplicates?

Count pairs with a specific sum


Write a function to count how many pairs in a sorted list add up to a given
target.
Example:

Input: arr = [1, 2, 3, 4, 4, 5], target = 8


Output: 2 # Pairs are [3, 5] and [4, 4]

6. Test yourself:

○ What happens if there are no valid pairs?


○ How do you handle duplicates?

Advanced Level
Find triplets with a specific sum
Write a function to find all unique triplets in a sorted list that add up to a
given target.
Example:

Input: arr = [-1, 0, 1, 2, -1, -4], target = 0


Output: [[-1, -1, 2], [-1, 0, 1]]

7. Test yourself:

○ How do you avoid duplicate triplets?


○ How do you handle cases with fewer than three numbers?

Container with most water


Imagine a list represents the heights of walls, where the distance between
each wall is 1. Find the two walls that can contain the most water. Use two
pointers to maximize the area.
Example:

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


Output: 49 # The walls at indices 1 and 8

8. Test yourself:

○ What happens if all heights are the same?


○ Can you explain why this method works?

Minimum window size for a sum


Find the smallest contiguous subarray in a list where the sum is greater
than or equal to a given target.
Example:

Input: arr = [2, 3, 1, 2, 4, 3], target = 7


Output: 2 # Subarray [4, 3]

9. Test yourself:

○ How do you handle negative numbers in the array?


○ What if no subarray meets the condition?

Maximum of all subarrays of size K


Given a list and a number K, find the maximum number in every sliding
window (subarray) of size K.
Example:
Input: arr = [1, 3, -1, -3, 5, 3, 6, 7], K = 3
Output: [3, 3, 5, 5, 6, 7]

10. Test yourself:

○ How do you efficiently handle large arrays?


○ Can you do this in a single pass?

How to Check If You’re Correct

For each question:

1. Write the code in Python.


2. Test the code with the example inputs and some edge cases (e.g.,
empty lists, duplicates, very large/small numbers).
3. Compare your output with the expected output.
4. If you’re stuck, try drawing what the left and right pointers are
doing at each step.

😊
Let me know if you want me to check your solutions or explain any specific
question further!

You might also like