Skip to main content

1. Two Sum

easyAsked at ServiceNow

Given an array of integers and a target, return indices of the two numbers that add up to target. ServiceNow asks this to confirm you reach for a hash map for O(n) lookups — the same pattern they use in ticket de-duplication and SLA pair-matching.

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

Source citations

Public interview reports confirming this problem appears in ServiceNow loops.

  • Glassdoor (2026-Q1)ServiceNow new-grad phone screens routinely lead with this.
  • LeetCode Discuss (2025-11)Posted as a ServiceNow warm-up across several SDE-I loops.

Problem

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

Constraints

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

Examples

Example 1

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

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

Example 2

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

Approaches

1. Brute force nested loop

Check every pair (i, j) and return when sum equals target.

Time
O(n^2)
Space
O(1)
function twoSum(nums, target) {
  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 — fine for n=10 but blows up at scale. ServiceNow ticket queues can hit millions of rows; the brute force makes auditors flinch.

2. Single-pass hash map

Walk once; for each value v, check if target - v already lives in the map. Otherwise store v with its index.

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);
  }
}

Tradeoff: Trades O(n) extra memory for O(n) time. Same shape ServiceNow uses to dedupe incident IDs against a watchlist of open tickets.

ServiceNow-specific tips

ServiceNow interviewers grade for hash-map fluency and clean variable names — they read this as a proxy for whether you'll write maintainable Glide-script-adjacent code. Bonus signal: mention that the hash-map pattern is exactly what their CMDB lookup uses to match incident records to configuration items.

Common mistakes

  • Returning the values [nums[i], nums[j]] instead of the indices [i, j].
  • Storing a value in the map BEFORE checking — fails when target = 2 * nums[i] and you reuse the same element.
  • Using a plain object {} as the map and tripping on negative-zero or numeric-string coercion.

Follow-up questions

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

  • What if the array is sorted? (Two-pointer becomes O(1) space.)
  • Return all pairs that sum to target.
  • Three-sum: extend to triples that sum to target.

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 one pass instead of two passes?

Two passes (build map, then scan) still works, but a single pass is cleaner and avoids the edge case where the same element appears twice with the target = 2x. The single-pass invariant 'map holds only indices strictly less than current i' makes that bug impossible.

Can I use a Set instead of a Map?

No — you need the original index, not just whether the complement existed. The Map stores value -> index.

Practice these live with InterviewChamp.AI

Drill Two Sum and other ServiceNow interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →