PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to design an efficient array-searching algorithm that maps values to indices under a target-sum constraint. It tests understanding of hash-based lookups versus brute-force pairwise comparison, a foundational data structures and algorithms topic in coding interviews. It is commonly used to assess time-complexity reasoning at an introductory to intermediate practical level.

  • medium
  • J.P. Morgan
  • Coding & Algorithms
  • Software Engineer

Two Sum — Indices Summing to a Target

Company: J.P. Morgan

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Two Sum — Indices Summing to a Target You are given an integer array `nums` and an integer `target`. Return the **indices** of the two numbers in `nums` that add up exactly to `target`. - You may assume that **each input has exactly one valid answer**. - You may **not** use the same element twice (the two indices must be different). - You may return the two indices in **any order**. ### Examples ``` Input: nums = [2, 7, 11, 15], target = 9 Output: [0, 1] Explanation: nums[0] + nums[1] = 2 + 7 = 9. Input: nums = [3, 2, 4], target = 6 Output: [1, 2] Explanation: nums[1] + nums[2] = 2 + 4 = 6. (Note: index 0 cannot pair with itself.) Input: nums = [3, 3], target = 6 Output: [0, 1] Explanation: The two distinct indices both hold the value 3. ``` ### Constraints - `2 <= nums.length <= 10^4` - `-10^9 <= nums[i] <= 10^9` - `-10^9 <= target <= 10^9` - Exactly one valid pair of indices exists. - Duplicate values may appear in `nums`; the two **indices** must still be distinct.

Quick Answer: This question evaluates a candidate's ability to design an efficient array-searching algorithm that maps values to indices under a target-sum constraint. It tests understanding of hash-based lookups versus brute-force pairwise comparison, a foundational data structures and algorithms topic in coding interviews. It is commonly used to assess time-complexity reasoning at an introductory to intermediate practical level.

You are given an integer array `nums` and an integer `target`. Return the **indices** of the two numbers in `nums` that add up exactly to `target`. - Each input has exactly one valid answer. - You may not use the same element twice (the two indices must be different). - You may return the two indices in any order. ### Examples ``` Input: nums = [2, 7, 11, 15], target = 9 Output: [0, 1] (nums[0] + nums[1] = 2 + 7 = 9) Input: nums = [3, 2, 4], target = 6 Output: [1, 2] (nums[1] + nums[2] = 2 + 4 = 6) Input: nums = [3, 3], target = 6 Output: [0, 1] (two distinct indices both hold the value 3) ``` ### Constraints - `2 <= nums.length <= 10^4` - `-10^9 <= nums[i] <= 10^9` - `-10^9 <= target <= 10^9` - Exactly one valid pair of indices exists. - Duplicate values may appear; the two indices must still be distinct.

Constraints

  • 2 <= nums.length <= 10^4
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • Exactly one valid pair of indices exists.
  • The two returned indices must be distinct.

Examples

Input: ([2, 7, 11, 15], 9)

Expected Output: [0, 1]

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

Input: ([3, 2, 4], 6)

Expected Output: [1, 2]

Explanation: nums[1] + nums[2] = 2 + 4 = 6; index 0 cannot pair with itself.

Input: ([3, 3], 6)

Expected Output: [0, 1]

Explanation: Two distinct indices both hold the value 3.

Input: ([-1000000000, 1000000000, 3, 5], 0)

Expected Output: [0, 1]

Explanation: Large opposite-sign values: -1e9 + 1e9 = 0.

Input: ([-3, 4, 3, 90], 0)

Expected Output: [0, 2]

Explanation: Negatives allowed: nums[0] + nums[2] = -3 + 3 = 0.

Input: ([1, 5, 5, 11], 10)

Expected Output: [1, 2]

Explanation: The matching pair uses the two duplicate 5s at distinct indices 1 and 2.

Hints

  1. A brute-force O(n^2) check of every pair works but is slow for n up to 10^4.
  2. For each element x, you need to find target - x. A hash map lets you look that up in O(1).
  3. Scan left to right, storing each value's index as you go. Before storing the current value, check whether its complement was already seen — this naturally guarantees the two indices are distinct.
Last updated: Jul 1, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • First Non-Repeating Character in a String - J.P. Morgan (medium)
  • Shift Non-Zero Elements Left In Place - J.P. Morgan (medium)
  • Can All Courses Be Completed? - J.P. Morgan (medium)
  • Merge Overlapping Intervals - J.P. Morgan (medium)
  • Implement Integer Square Root - J.P. Morgan (medium)