Question 1
EasyWhat is the two-pointer technique?
- A.
Running two loops nested
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the two-pointer technique?
Running two loops nested
Using two indices that move through a data structure, often from both ends or at different speeds
Using two arrays
Using pointers in C only
Answer: B
It often turns an O(n²) pair search into O(n), for example on a sorted array.
Which problem is naturally solved with two pointers starting at both ends of a sorted array?
Finding a pair with a given sum
Reversing a linked list
Finding the maximum element
Counting elements
Answer: A
If the sum is too small, move the left pointer right; if too large, move the right pointer left.
What is the output of this code?
s = "racecar"
i, j = 0, len(s) - 1
ok = True
while i < j:
if s[i] != s[j]:
ok = False
break
i += 1
j -= 1
print(ok)
False
None
Answer: D
Comparing characters from both ends inward checks for a palindrome: 'racecar' matches all the way.
What is a sliding window?
A sorted subarray
A recursive call
A range of elements that moves across the array, updated incrementally
A GUI window
Answer: C
Adding the new element and removing the old one avoids recomputing each window from scratch.
What is the output of this code?
a, b = [1, 3, 5], [2, 4]
i = j = 0
out = []
while i < len(a) and j < len(b):
if a[i] <= b[j]:
out.append(a[i]); i += 1
else:
out.append(b[j]); j += 1
out += a[i:] + b[j:]
print(out)
[5, 4, 3, 2, 1]
[1, 3, 5, 2, 4]
Answer: D
Merging two sorted lists with two pointers produces one sorted list.
What is the time complexity of finding a pair with a given sum in a sorted array using two pointers?
O(n)
O(log n)
O(n log n)
O(n²)
Answer: A
Each step moves one pointer inward, so there are at most n steps.
In the fast–slow pointer technique, how fast does the fast pointer usually move?
Ten steps
Two steps for each step of the slow pointer
Same speed
Half the speed
Answer: B
It is used to find the middle of a list or to detect a cycle.
Using two pointers at both ends of the sorted array [1, 2, 4, 7, 11], which pair is found with sum 15?
4 and 7
4 and 11
2 and 11
7 and 11
Answer: B
1 + 11 = 12 < 15, move left; 2 + 11 = 13 < 15, move left; 4 + 11 = 15. Found.
In the sorted pair-sum method with pointers at both ends, what do you do when the current sum is less than the target?
Move the right pointer one step left
Move both pointers
Stop: no pair exists
Move the left pointer one step right
Answer: D
Moving right from the left end picks a larger number, increasing the sum.
In the sorted pair-sum method, what do you do when the current sum is greater than the target?
Move the left pointer right
Move the right pointer one step left
Restart from the beginning
Swap the two elements
Answer: B
Moving left from the right end picks a smaller number, decreasing the sum.
Error
True
[2, 4, 1, 3, 5]
[1, 2, 3, 4, 5]