Question Bank

Two-Pointer Warm-Up

Difficulty: Easy

Four short prompts covering pair sums, in-place dedupe, and palindrome checks with two pointers. Great preparation before sliding-window drills.

Question Bank
/

Two-Pointer Warm-Up

Two-Pointer Warm-Up

Four short prompts covering pair sums, in-place dedupe, and palindrome checks with two pointers. Great preparation before sliding-window drills.

Question Bank
Easy
JavaScript
4 questions
two-pointers
algorithms
quiz
fundamentals

946 views

31

Implement hasPairSum(sortedArr, target) that returns true if any two distinct elements in the sorted array sum to target. Use the two-pointer technique in O(n) time.

Examples

Example 1:

Input: sortedArr = [1, 2, 4, 7, 11], target = 9
Output: true
Explanation: lo = 0, hi = 4. Sum 1 + 11 = 12 > 9, hi--. Sum 1 + 7 = 8 < 9, lo++. Sum 2 + 7 = 9, return true.

Example 2:

Input: sortedArr = [1, 2, 4, 7, 11], target = 20
Output: false
Explanation: The pointers converge without ever hitting the target, so the function returns false. O(n) time, O(1) space.