Skip to main content

1. Two Sum

easyAsked at Workday

Given an array of integers and a target, return the indices of the two numbers that add up to the target. Workday uses this to evaluate whether you reach for a hash map instead of brute-force enumeration when reconciling payroll line items.

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

Source citations

Public interview reports confirming this problem appears in Workday loops.

  • Glassdoor (2025)Workday phone-screen reports list this as the warmup before payroll-reconciliation followups.
  • LeetCode Discuss (2026-Q1)Cited as a Workday SDE intern screen.

Problem

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Return the answer in any order.

Constraints

  • 2 <= nums.length <= 10^4
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • Exactly one valid answer exists.

Examples

Example 1

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

Explanation: nums[0] + nums[1] = 2 + 7 = 9.

Example 2

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

Approaches

1. Brute force pairs

Check every pair (i, j) where i < j.

Time
O(n^2)
Space
O(1)
for (let i = 0; i < nums.length; i++) {
  for (let j = i+1; j < nums.length; j++) {
    if (nums[i] + nums[j] === target) return [i, j];
  }
}

Tradeoff: Quadratic — at payroll scale (millions of line items per period) this is a non-starter.

2. Hash map single pass

Store seen values keyed by value, look up complement in O(1).

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

Tradeoff: Single pass, O(n). The trick is checking before inserting so you never pair a value with itself.

Workday-specific tips

Workday interviewers grade on whether you state the trade between memory and time before coding. They also value defensive code: handle empty arrays, duplicates (which payroll has lots of — same dollar amount across employees), and confirm the 'one solution' assumption out loud.

Common mistakes

  • Inserting into the map before checking — you'll pair an element with itself when nums[i] * 2 == target.
  • Returning values instead of indices.
  • Forgetting that duplicates are allowed in nums but you can't reuse the same index.

Follow-up questions

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

  • What if the array is sorted? (Two pointers, O(1) space.)
  • What if you need all pairs, not just one?
  • Three Sum — extend to triples.

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 check before inserting?

If you insert first, then a value like nums[i] = 3 with target = 6 would match itself. Checking complement first ensures both indices are distinct.

Can the same value appear twice in nums?

Yes — e.g., nums = [3,3], target = 6 → [0,1]. The hash-map approach still works because the second insertion overwrites the first, but you've already returned by then.

Practice these live with InterviewChamp.AI

Drill Two Sum 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 →