Quick Overview

Solve Two Sum with a complement map while preserving distinct original indices. Explain duplicate handling, lookup-before-insertion, correctness, and the tradeoff against sorting or pairwise search.

Find the Two Indices Whose Values Sum to a Target

Company: Bnsf

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

Given an integer array and a target, return the indices of two distinct elements whose sum equals the target. ### Constraints and Clarifications The report names Two Sum without additional variant details. Use the [official reference](https://leetcode.com/problems/two-sum/description/) for this practice contract: 2 <= n <= 10000, values and target between -1000000000 and 1000000000, and exactly one valid unordered index pair. These are reference-context assumptions, not separately reported interview constraints. Return the two zero-based indices in increasing order as an output-normalization convention. Do not use the same element twice. ### Example For [4,1,6] and target 10, return [0,2]. For [5,5] and target 10, return [0,1]. ### What a Strong Answer Covers Improve on pairwise enumeration with a lookup map, explain why lookup comes before insertion, preserve original indices, and account for duplicate values. ### Follow-up Questions - How would the contract change if there were no solution or several valid pairs? - What tradeoff would a sorting-based approach introduce?

Overview: Solve Two Sum with a complement map while preserving distinct original indices. Explain duplicate handling, lookup-before-insertion, correctness, and the tradeoff against sorting or pairwise search.

Given an integer array nums and an integer target, return the indices of two distinct elements whose sum equals target. Use the official Two Sum reference as the practice domain: there are 2 through 10000 elements, every value and the target lies between -1000000000 and 1000000000, and exactly one unordered pair of distinct indices is valid. Return that pair of zero-based indices in increasing order. The official-reference assumptions are the practice contract, not separately reported interview constraints. Example: nums = [4, 1, 6], target = 10 returns [0, 2]. nums = [5, 5], target = 10 returns [0, 1].

Constraints

  • 2 <= len(nums) <= 10000.
  • -1000000000 <= nums[i] <= 1000000000 and -1000000000 <= target <= 1000000000.
  • Exactly one unordered pair of distinct indices sums to target.
  • Return two zero-based indices in increasing order; never reuse one element.
  • These bounds and uniqueness are official-reference practice assumptions, not separately reported interview constraints.

Examples

Input: ([4, 1, 6], 10)

Expected Output: [0, 2]

Explanation: The first and last values add to ten.

Input: ([5, 5], 10)

Expected Output: [0, 1]

Explanation: Two equal values at distinct positions form the pair.

Hints

  1. The two chosen positions must be different.
  2. The output order is determined by the positions, not by the values.

Loading coding console...

Show the approach

Approach

Scan the input left to right, storing the earliest index of each value already visited. Before inserting the current value, look up target minus that value. If found, the stored index is earlier than the current one, so returning the two indices in that order meets the output convention. Because exactly one valid unordered pair exists, this first found pair is necessarily the unique answer. Checking before insertion prevents the current element from pairing with itself. Duplicate values work: the first copy is stored, and a second copy can complete the pair. Every value and target is within one billion in magnitude, so the complement lies within signed 32-bit range. The guaranteed-pair domain makes the empty fallback unreachable.

Time complexity:
O(n) expected time
Space complexity:
O(n) auxiliary space