
Three transactions that cancel out
hardThree transactions that cancel out
Stripe Python Interview Question
Stripe's reconciliation team looks for groups of three ledger entries whose amounts add up to exactly zero, since they often point to a refund split across two payments.
Write a function three_sum(amounts) that returns every unique group of three amounts, taken from different positions in the list, that adds up to 0. Sort the amounts inside each group from smallest to largest, sort the groups, and do not repeat a group.
Asked of
- Data Analyst
- Data Engineer
- Data Scientist
- ML Engineer
- AI Engineer
Example 1
Input
amounts = [-1, 0, 1, 2, -1, -4]
Output
[[-1, -1, 2], [-1, 0, 1]]
Example 2
Input
amounts = [1, 2, -2, -1]
Output
[]
Explanation
In the first example, -1 + -1 + 2 and -1 + 0 + 1 both add up to 0. The second -1 could make another [-1, 0, 1] group, but each group is listed once. In the second example, no three amounts add up to 0.
Submit also runs 4 hidden test cases that check edge cases.
Company
Stripe
Difficulty
hard
Topic
two pointers
Language
Python
Your code
Loading Python in the background. You can start writing.