Question 1
what will be the position of the pointers left, right after executing the above code?
#include <iostream> #include <vector> using namespace std; int main() { vector<int> arr = {1, 2, 3, 4, 5}; int left = 0, right = 4; while (arr[left] < arr[right]) { left++; right--; } return 0; }
#include <stdio.h> int main() { int arr[] = {1, 2, 3, 4, 5}; int left = 0, right = 4; while (arr[left] < arr[right]) { left++; right--; } return 0; }
public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; int left = 0, right = 4; while (arr[left] < arr[right]) { left++; right--; } } }
arr = [1, 2, 3, 4, 5] left = 0 right = 4 while arr[left] < arr[right]: left += 1 right -= 1
let arr = [1, 2, 3, 4, 5]; let left = 0, right = 4; while (arr[left] < arr[right]) { left++; right--; }
2, 2
2, 1
3, 2
2, 3
Question 2
Why is two pointers more efficient than nested loops for pair-sum in sorted arrays?
It avoids extra memory
Each element is processed at most once
It uses recursion
It skips sorting
Question 3
Given the string s = "racecar"
, which technique can be used to efficiently check if the string is a palindrome?
Traversing the string from left to right
Using two pointers: one starting at the leftmost character and one at the rightmost, and checking for equality while moving inward.
Using recursion to compare characters at each position
None of the Above
Question 4
Given the sorted array arr = [2, 4, 6, 8, 10]
and a target sum of 12
, what will be the action when the two pointers approach a pair whose sum is less than 12
?
Move the right
pointer leftward
Move the left
pointer rightward
Both pointers move inward
None of the above
Question 5
How would you implement the two-pointer technique to merge two sorted arrays into a single sorted array?
Consider the arrays arr1 = [1, 3, 5]
and arr2 = [2, 4, 6]
. Which of the following steps correctly describes the process?
Start both pointers at the end of the arrays, compare the elements at the pointers, and append the larger element to the result.
Start both pointers at the beginning of the arrays, compare the elements at the pointers, append the smaller element to the result, and move the corresponding pointer forward.
Start both pointers at the beginning of the arrays, append both elements at the pointers to the result, and move both pointers forward.
Start both pointers at the middle of the arrays, compare the elements at the pointers, and append the larger element to the result.
Question 6
In a singly linked list, how would you implement the two-pointer technique to find the middle element of the list?
Move both pointers at the same pace, and the second pointer will always be ahead of the first pointer. When the second pointer reaches the end, the first pointer will be at the middle.
Start both pointers at the head, and move the first pointer to the right and the second pointer to the left. The middle will be the average of the two pointers.
Start both pointers at the tail of the linked list, and move both pointers toward the head at the same pace.
Start both pointers at the head, and move the second pointer twice as fast as the first pointer. When the second pointer reaches the end, the first pointer will be at the middle.
Question 7
How would you implement the two-pointer technique to reverse a string str = "Pointer"
?
Use one pointer at the beginning of the string and the other at the end, and swap the characters at these positions until the pointers meet in the middle.
Use one pointer to iterate through the string and shift each character to its opposite end.
Use two pointers starting at the beginning and the middle of the string, and swap elements until the middle is reached.
Use two pointers at the same position and swap the characters one at a time.
Question 8
Consider an array consisting of –ve and +ve numbers. What would be the worst case time complexity to segregate the numbers having same sign altogether i.e all +ve on one side and then all -ve on the other using two pointer technique?
O(N log N)
O(N)
O(N * N)
O(log N)
Question 9
What will be the time complexity of the algorithm using two pointers to find the triplet with sum equal to zero.
Example : arr = { 1, 3, -5, 7, 3 , -2}
Triplet : {-5 , 7, -2} = 0
O(n)
O(n Log n)
O(n^3)
O(n^2)
Question 10
Given arr = [2, 3, 1, 2, 4, 3]
and target 7
, what is the length of the smallest subarray with sum ≥ 7?is the purpose of this pseudo code?
2
3
4
5
There are 13 questions to complete.