Find the Unique Pair of Array Indices Whose Values Sum to a Target

Quick Overview

Given an integer array and a target, return the indices of the only pair of different elements whose values add up to the target, smaller index first. Arrays hold up to 10,000 values of up to one billion in magnitude, and the task is a common warm-up that tests clean, efficient array reasoning.

Find the Unique Pair of Array Indices Whose Values Sum to a Target

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Given an array of integers `nums` and an integer `target`, return the indices of the two different elements whose values add up to `target`. ### Function Signature ```python def two_sum(nums: list[int], target: int) -> list[int]: ``` ### Rules - Exactly one pair of indices `i < j` satisfies `nums[i] + nums[j] == target`. - Return that pair as `[i, j]` with the smaller index first. - An element cannot be paired with itself, although equal values at different indices may form the pair. ### Constraints - `2 <= len(nums) <= 10000` - `-10^9 <= nums[i] <= 10^9` - `-10^9 <= target <= 10^9` - Exactly one valid pair 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]` Explanation: `2 + 4 = 6`; index 0 cannot be used twice to make `3 + 3`. **Example 3** Input: `nums = [3, 3]`, `target = 6` Output: `[0, 1]`

Overview: Given an integer array and a target, return the indices of the only pair of different elements whose values add up to the target, smaller index first. Arrays hold up to 10,000 values of up to one billion in magnitude, and the task is a common warm-up that tests clean, efficient array reasoning.

|Home/Coding & Algorithms/Meta
Meta logo
Meta
Sep 14, 2026
mediumSoftware EngineerOnsiteCoding & Algorithms
1
0

Given an array of integers nums and an integer target, return the indices of the two different elements whose values add up to target.

Function Signature

def two_sum(nums: list[int], target: int) -> list[int]:

Rules

  • Exactly one pair of indices i < j satisfies nums[i] + nums[j] == target .
  • Return that pair as [i, j] with the smaller index first.
  • An element cannot be paired with itself, although equal values at different indices may form the pair.

Constraints

  • 2 <= len(nums) <= 10000
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • Exactly one valid pair 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]

Explanation: 2 + 4 = 6; index 0 cannot be used twice to make 3 + 3.

Example 3

Input: nums = [3, 3], target = 6

Output: [0, 1]

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...