
RAYS Coding
June 5, 2025 at 07:15 PM
Now, let’s move to the next Interview Coding Pattern in the Python Learning Series:
For those of you who recently joined this channel, you should first go through:
Python Learning Series: https://whatsapp.com/channel/0029VaiM08SDuMRaGKd9Wv0L/1527
Interview Coding Patterns: https://whatsapp.com/channel/0029VaiM08SDuMRaGKd9Wv0L/1629
*Two Pointer Technique*
*Why interviewers ask this?*
The Two Pointer technique is very common in array and string problems, especially when you're trying to optimize for time complexity. It's often used in:
- Sorted arrays
- Finding pairs/triplets with certain conditions
- Merging arrays
- String manipulation problems
It shows the interviewer that you can write optimized solutions without brute force.
*Use this technique when:*
You have to compare or pair elements in a sequence.
You're solving problems like:
- Pair sum in sorted array
- Reversing strings or arrays
- Moving zeros to the end
- Sorting squares of a sorted array
- Checking for palindromes
*Types of Two Pointer Techniques:*
*1. Opposite Direction:*
Start one pointer at the beginning and one at the end, then move toward the center.
Example: Finding if a pair in a sorted array adds up to a target.
*2. Same Direction:*
Start both pointers from the beginning and move forward.
Example: Removing duplicates, merging arrays.
Let’s discuss some important coding interview problems:
*Problem: Pair with Target Sum (in sorted array)*
> Given a sorted array of integers and a target value, return whether there exists a pair of numbers that adds up to the target.
*Example:*
arr = [1, 2, 4, 6, 10]
target = 8
*Approach:*
Use one pointer from the start and one from the end. Move them based on the current sum.
*Code*:
def has_pair_with_sum(arr, target):
left = 0
right = len(arr) - 1
while left < right:
current_sum = arr[left] + arr[right]
if current_sum == target:
return True
elif current_sum < target:
left += 1
else:
right -= 1
return False
*Explanation:*
left starts from the beginning; right from the end.
You calculate current = arr[left] + arr[right].
If current == target: You've found the pair!
If current < target: Move left forward to increase the sum.
If current > target: Move right backward to decrease the sum.
Keep moving until pointers meet. If no pair is found, return False.
*Why this works better than brute force?*
Brute force = O(n²)
Two pointers = O(n)
You avoid checking every pair.
*2. Remove Duplicates from Sorted Array*
*Problem:*
Given a sorted array, remove the duplicates in-place such that each element appears only once.
*Example:*
[1, 1, 2, 2, 3] → [1, 2, 3]
*Approach:*
Use two pointers — one for placing the unique value, and the other for traversing.
*Code:*
def remove_duplicates(nums):
if not nums:
return 0
slow = 0
for fast in range(1, len(nums)):
if nums[fast] != nums[slow]:
slow += 1
nums[slow] = nums[fast]
return slow + 1 # length of unique elements
*3. Reverse a String or Array*
*Problem:*
Reverse a string or array in-place.
*Example:*
["h", "e", "l", "l", "o"] → ["o", "l", "l", "e", "h"]
*Code:*
def reverse_string(s):
left, right = 0, len(s) - 1
while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1
*4. Move Zeros to the End*
*Problem:*
Move all zeros in an array to the end while maintaining the order of non-zero elements.
*Example:*
[0, 1, 0, 3, 12] → [1, 3, 12, 0, 0]
*Code:*
def move_zeros(nums):
non_zero = 0
for i in range(len(nums)):
if nums[i] != 0:
nums[non_zero], nums[i] = nums[i], nums[non_zero]
non_zero += 1
*5. Check if a String is a Palindrome*
*Problem:*
Check if a string is the same when reversed.
*Example:*
"madam" → True, "racecar" → True, "hello" → False
*Code:*
def is_palindrome(s):
left, right = 0, len(s) - 1
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True
*6. Sorted Squares of a Sorted Array*
*Problem:*
Return the squares of a sorted array, also sorted.
*Example:*
[-4, -1, 0, 3, 10] → [0, 1, 9, 16, 100]
*Approach:*
Two pointers from both ends — place the larger square at the end.
Code:
def sorted_squares(nums):
result = [0] * len(nums)
left, right = 0, len(nums) - 1
index = len(nums) - 1
while left <= right:
if abs(nums[left]) > abs(nums[right]):
result[index] = nums[left] ** 2
left += 1
else:
result[index] = nums[right] ** 2
right -= 1
index -= 1
return result
*Two Pointer technique is one of the easiest ways to optimize problems involving arrays or strings — especially when working with sorted data or search-based problems.*
*React with ❤️ once you're ready for next quiz on this topic*