Skip to main content

21. Two Sum II - Input Array Is Sorted

easyAsked at Salesforce

Find two indices in a sorted array that sum to a target, using O(1) space. Salesforce uses this to test the two-pointer pattern that exploits sortedness.

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

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Asked after Two Sum I as a 'can you exploit the sortedness?' follow-up.
  • Blind (2025)Salesforce uses this to test space-time trade-off recognition.

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, added by one, 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]

Approaches

1. Hash map (ignoring sortedness)

Same as LC 1 — store seen values in a map.

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: Wastes the sorted invariant and uses O(n) space, violating the constraint.

2. Two pointers from ends

lo at start, hi at end. If sum < target, lo++. If sum > target, hi--. Else return.

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

Tradeoff: True O(1) space. The sorted invariant guarantees each step makes monotone progress toward the answer.

Salesforce-specific tips

Salesforce specifically grades on whether you exploit sortedness — going straight to the hash map shows you didn't read the problem. Bonus signal: explain WHY the two-pointer step is correct (if sum is too big, the smallest hi-mate makes it even bigger, so hi can never partner with current lo).

Common mistakes

  • Returning 0-indexed answers — problem says 1-indexed.
  • Using lo <= hi — would allow comparing an element to itself.
  • Reaching for the hash map without thinking about sortedness.

Follow-up questions

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

  • Three Sum (LC 15).
  • Two Sum III - data structure design (LC 170).
  • Two Sum on a BST (LC 653).

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 the two-pointer step terminate?

lo only increases, hi only decreases. After O(n) iterations they meet, at which point no pair is possible. The unique-solution guarantee means we always find the answer first.

Why can't I use the same element twice?

Problem constraint. The two pointers start at different ends and the lo < hi check prevents them from coinciding.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →