PracHub
QuestionsLearningGuidesInterview Prep
|Home/Coding & Algorithms/J.P. Morgan

Return Every Index Pair Whose Values Sum to a Target

Last updated: Aug 5, 2026

Quick Overview

Return every distinct pair of array indices whose values add to a target, including all valid combinations created by duplicates. Skills under review include deterministic ordering, hash-based reasoning, and complexity analysis that accounts for potentially large output.

  • medium
  • J.P. Morgan
  • Coding & Algorithms
  • Machine Learning Engineer

Return Every Index Pair Whose Values Sum to a Target

Company: J.P. Morgan

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Return Every Index Pair Whose Values Sum to a Target ### Problem Implement `all_pairs_sum(nums, target)`. Given an unordered array of integers, return every pair of distinct zero-based indices whose values sum to `target`. ### Output Contract - Represent a pair as `[i, j]` with `i < j`. - Return each index pair exactly once. - Treat equal values at different indices as distinct elements. - Sort the result lexicographically by `i` and then `j`. ### Examples ```text nums = [1, 3, 2, 2, 3] target = 4 result = [[0, 1], [0, 4], [2, 3]] ``` ```text nums = [0, 0, 0] target = 0 result = [[0, 1], [0, 2], [1, 2]] ``` ### Requirements - Use a hash-based approach rather than checking every pair directly. - Preserve every prior index for a value; a single stored index is insufficient when duplicates occur. - Account for the fact that the output itself may contain a quadratic number of pairs. - State the expected time and space complexity in terms of `n`, the input length, and `P`, the number of returned pairs. ```hint Use duplicates as a test case Before choosing what each hash-map entry stores, trace `[2, 2, 2]` and list every distinct index pair that must survive. ``` ### Discussion Prompts 1. What are the keys and values in the hash map? 2. Why must each value be a list of indices rather than one index? 3. Why is the current index inserted only after its complement has been processed? 4. When does the running time exceed linear even though hash lookup is expected constant time? 5. What are the trade-offs relative to the direct quadratic scan?

Quick Answer: Return every distinct pair of array indices whose values add to a target, including all valid combinations created by duplicates. Skills under review include deterministic ordering, hash-based reasoning, and complexity analysis that accounts for potentially large output.

Related Interview Questions

  • Two Sum — Indices Summing to a Target - J.P. Morgan (medium)
  • 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)
|Home/Coding & Algorithms/J.P. Morgan

Return Every Index Pair Whose Values Sum to a Target

J.P. Morgan logo
J.P. Morgan
Aug 5, 2026, 12:00 AM
mediumMachine Learning EngineerTechnical ScreenCoding & Algorithms
1
0

Return Every Index Pair Whose Values Sum to a Target

Problem

Implement all_pairs_sum(nums, target). Given an unordered array of integers, return every pair of distinct zero-based indices whose values sum to target.

Output Contract

  • Represent a pair as [i, j] with i < j .
  • Return each index pair exactly once.
  • Treat equal values at different indices as distinct elements.
  • Sort the result lexicographically by i and then j .

Examples

nums = [1, 3, 2, 2, 3]
target = 4
result = [[0, 1], [0, 4], [2, 3]]
nums = [0, 0, 0]
target = 0
result = [[0, 1], [0, 2], [1, 2]]

Requirements

  • Use a hash-based approach rather than checking every pair directly.
  • Preserve every prior index for a value; a single stored index is insufficient when duplicates occur.
  • Account for the fact that the output itself may contain a quadratic number of pairs.
  • State the expected time and space complexity in terms of n , the input length, and P , the number of returned pairs.

Discussion Prompts

  1. What are the keys and values in the hash map?
  2. Why must each value be a list of indices rather than one index?
  3. Why is the current index inserted only after its complement has been processed?
  4. When does the running time exceed linear even though hash lookup is expected constant time?
  5. What are the trade-offs relative to the direct quadratic scan?

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More J.P. Morgan•More Machine Learning Engineer•J.P. Morgan Machine Learning Engineer•J.P. Morgan Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

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

Product

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

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.