Skip to main content

22. Two Sum II - Input Array Is Sorted

easyAsked at Reddit

Find two numbers in a sorted array that sum to a target. Reddit uses this two-pointer variant to test whether candidates recognize when sorting buys them O(1) extra space — relevant for sorted vote-tally pairing.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit phone screen, often the follow-up to Two Sum.

Problem

Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Return the indices of the two numbers (1-indexed) as an integer array of length 2. The tests are generated such that there is exactly one solution. You may not use the same element twice. Your solution must use only constant extra space.

Constraints

  • 2 <= numbers.length <= 3 * 10^4
  • -1000 <= numbers[i] <= 1000
  • numbers is sorted in non-decreasing order.
  • -1000 <= target <= 1000

Examples

Example 1

Input
numbers = [2,7,11,15], target = 9
Output
[1,2]

Example 2

Input
numbers = [2,3,4], target = 6
Output
[1,3]

Example 3

Input
numbers = [-1,0], target = -1
Output
[1,2]

Approaches

1. Hash map (ignore sorted)

Use the same hash-map trick as Two Sum. Ignores the sorted property.

Time
O(n)
Space
O(n)
function twoSum(numbers, target) {
  const seen = new Map();
  for (let i = 0; i < numbers.length; i++) {
    const need = target - numbers[i];
    if (seen.has(need)) return [seen.get(need) + 1, i + 1];
    seen.set(numbers[i], i);
  }
}

Tradeoff: Linear time but O(n) space — fails the constant-space requirement.

2. Two pointers (optimal)

Pointers from both ends. If sum < target, move left++; if sum > target, move right--.

Time
O(n)
Space
O(1)
function twoSum(numbers, target) {
  let l = 0, r = numbers.length - 1;
  while (l < r) {
    const sum = numbers[l] + numbers[r];
    if (sum === target) return [l + 1, r + 1];
    if (sum < target) l++;
    else r--;
  }
}

Tradeoff: Constant space. Exploits the sorted property. The 1-indexed return is a classic off-by-one trap.

Reddit-specific tips

Reddit interviewers grade on whether you exploit the sorted property without prompting. Bonus signal: explain WHY the two-pointer works (monotonicity) and connect to merging sorted vote-tallies across shards.

Common mistakes

  • Returning 0-indexed answers (problem is 1-indexed).
  • Using hash map when constant-space is required.
  • Failing the equal-elements case (must not use the same element twice).

Follow-up questions

An interviewer at Reddit may pivot to one of these next:

  • 3Sum (LC 15) — extends two-pointer to a 3-loop.
  • 4Sum (LC 18) — same generalization.
  • Two sum with multiple solutions — collect all pairs.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why does two-pointer work here but not on unsorted?

Sorted means moving left up increases sum, moving right down decreases. That monotonicity is what binds the search space.

What if there are duplicates and multiple solutions?

Skip duplicates when advancing pointers, like the 3Sum pattern.

Practice these live with InterviewChamp.AI

Drill Two Sum II - Input Array Is Sorted and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →