Two Sum: All Pairs
Updated
Problem statement
You are given an integer array nums and an integer target. Find every pair of values in nums that adds up to target.
The two values of a pair must come from two different positions in the array, so a value can pair with itself only when it appears at least twice. Return each pair of values only once, even when several pairs of positions give it.
Write each pair as [a, b] with a <= b, and return the pairs sorted by a in increasing order. If no two values add up to target, return an empty array.
Function
findAllPairs(nums: int[], target: int) → int[][]Examples
Example 1
nums = [3,8,5,1,7,4]target = 9return = [[1,8],[4,5]]Two pairs add up to 9: 1 + 8 and 4 + 5. Each pair is written smaller value first, and [1, 8] comes before [4, 5] because 1 < 4. The values 3 and 7 have no partner, because 6 and 2 are not in the array.
Example 2
nums = [2,6,2,6,4,4,4]target = 8return = [[2,6],[4,4]]The positions of the two 2s and the two 6s give four pairs of positions that add up to 8, but they all give the same values, so [2, 6] is returned once. The value 4 appears three times, so it can pair with itself, and [4, 4] is also returned once.
Example 3
nums = [5,1,3]target = 10return = []5 + 5 = 10, but 5 appears only once, and a position cannot be used twice. No other two values add up to 10, so the answer is an empty array.
Example 4
nums = [-4,0,10,-6,4]target = 4return = [[-6,10],[0,4]]Negative values pair like any other: -6 + 10 = 4 and 0 + 4 = 4. The pair starting with -6 comes first. -4 would need 8, which is not in the array.
Constraints
2 <= nums.length <= 104-109 <= nums[i] <= 109-109 <= target <= 109
More Zillow Phone Screen questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Time Based Key-Value StorePHONE SCREEN · Seen Feb 2024
- Filter Formatted Records by CriteriaPHONE SCREEN · Seen Sep 2023
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Jump GamePHONE SCREEN · Seen Dec 2022
- Reversible Run-Length EncodingPHONE SCREEN · Seen Nov 2022
- Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
- Employee Free TimePHONE SCREEN · Seen Oct 2022
- First Unique Character in a StringPHONE SCREEN · Seen Oct 2022
- K-th Smallest Number in a StreamPHONE SCREEN · Seen Oct 2022
Similar questions at other companies
- Merchant Payout Reserve CalculatorStripe · PHONE SCREENStripe interview questions
- Price CheckBlue Origin · PHONE SCREEN
- Majority ElementByteDance · PHONE SCREENByteDance interview questions
- Detect a Straight FlushTemu · PHONE SCREENTemu interview questions
- Recommend Films from Viewers Who Watched Everything You DidBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Find Unique ElementsCisco · PHONE SCREENCisco interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Two SumAmazon · OA · PHONE SCREEN
- Employee Count Median, Mean and ModeClay · PHONE SCREEN
- Two SumBNSF Railway · PHONE SCREENBNSF Railway interview questions