Two Expense Lines That Add Up to a Target
Updated
Problem statement
An expense report has a list of amounts nums, one per line, and a reviewer is looking for two lines whose amounts add up to target.
Find the two different positions i and j with nums[i] + nums[j] == target. A single line cannot be used twice, but two different lines may hold the same amount.
Exactly one such pair exists. Return [i, j] with the smaller position first, so i < j. Positions start at 0.
Function
twoSum(nums: int[], target: int) → int[]Examples
Example 1
nums = [14,-6,9,30,1]target = 24return = [1,3]-6 + 30 = 24, and no other two amounts add up to 24. Their positions are 1 and 3, smaller first.
Example 2
nums = [5,12,5,40]target = 10return = [0,2]The two 5s sit at positions 0 and 2 and add up to 10. One 5 cannot be used twice, and no other pair works.
Example 3
nums = [-8,-3,0,7]target = -11return = [0,1]-8 + (-3) = -11. Amounts can be negative, and so can the target.
Constraints
2 <= nums.length <= 10000-1000000000 <= nums[i] <= 1000000000-1000000000 <= target <= 1000000000- Exactly one pair of positions
i < jhasnums[i] + nums[j] == target.
More Rippling Onsite Interview questions
- Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Camel CardsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Limit an Organization Tree's HeightPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Delivery Cost TrackerPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Maximum Currency Conversion Along a Simple PathPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Top K Frequent WordsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Future-Effective Employee SalaryONSITE INTERVIEW · Seen Aug 2026
- Filter and Sort Scheduled TasksONSITE INTERVIEW · Seen Jul 2026
Similar questions at other companies
- List the Key and Value of Every Map EntryNewgen Software · ONSITE INTERVIEW
- Pizza Price CalculatorMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Cross-Day User Trust ScoresAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Filter Pins by Excluded IDsPinterest · ONSITE INTERVIEWPinterest interview questions
- Best Average Student ScoreGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Longest Dictionary Word from Nine LettersGoogle · ONSITE INTERVIEWGoogle interview questions
- Two SumOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
- Keep the First Key for Each Distinct ValueOneTrust · ONSITE INTERVIEW
- Two Positions That Add Up to a TargetDocuSign · PHONE SCREEN · ONSITE INTERVIEWDocuSign interview questions
Interview reports that mention this question
Rippling interview experiences
- Rippling No-Level Onsite Loops: 24 Experiences (Part 5 of 6)
- Rippling No-Level Screens and Loops: 18 Experiences (Part 4 of 6)
- Rippling No-Level Coding Screens: 37 Experiences (Part 3 of 6)
- Rippling No-Level Design, Screens: 30 Experiences (Part 2 of 6)
- Rippling SWE Interviews, No Level: 156 Experiences (Part 1 of 6)