Skip to main content

22. Two Sum II - Input Array Is Sorted

mediumAsked at Workday

Find two numbers in a 1-indexed sorted array that sum to a target. Workday uses this to test two-pointer pattern recognition on sorted ledger data — common shape for paid-vs-owed reconciliations.

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

Source citations

Public interview reports confirming this problem appears in Workday loops.

  • Glassdoor (2025)Workday SDE2 phone screen — sorted-array warmup.

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. Return the indices of the two numbers (1-indexed) as an integer array [index1, index2] of length 2.

Constraints

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

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 (ignores sorted)

Same as Two Sum I — store seen values.

Time
O(n)
Space
O(n)
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: Works but the prompt explicitly demands O(1) space.

2. Two pointers from both ends

lo=0, hi=n-1. 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--;
  }
  return [];
}

Tradeoff: O(1) space. The sortedness gives us the monotonic direction to move pointers.

Workday-specific tips

Workday grades whether you spot that sortedness lets you drop the hash map. State 'because the array is sorted, two pointers is O(1) space' before coding. Don't forget the 1-indexing — they'll dock points for off-by-one.

Common mistakes

  • Returning 0-indexed values — prompt says 1-indexed.
  • Reaching for a hash map — works but uses O(n) extra space, violating the prompt.
  • Returning when lo == hi — that's using the same element twice.

Follow-up questions

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

  • Three Sum (LC 15) — fix one element, run two-pointer on the rest.
  • Two Sum (LC 1) — unsorted version.
  • What if there are multiple solutions?

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 two pointers and not binary search?

Two pointers is O(n) and binary search at each step would be O(n log n). Both work; two pointers is the canonical sorted-array pattern.

What if duplicates?

Two pointers still works — just keep the first match you find. The prompt guarantees a unique pair, so duplicates don't trip us.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →