Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Accounts receivable has a payment that does not match any single invoice line. The usual reason is that the customer paid three lines at once, so the reconciliation job wants every combination of three line amounts that adds up exactly to the payment. Amounts are in cents, lines arrive in no particular order, the same amount often appears on several lines, and credits are negative. The job first finds the candidate amount combinations and then looks up which lines carry those amounts, so each combination of amounts is reported once, not once per set of lines.
Write three_line_totals(amounts_cents, total_cents). It takes a list of integer line amounts in cents, in any order, and an integer total in cents, and returns every distinct triplet of amounts [a, b, c] that sums exactly to total_cents.
Function to write
three_line_totals(amounts_cents: list[int], total_cents: int) -> list[list[int]]A sorted list of distinct [a, b, c] amount triplets (a <= b <= c) that sum to total_cents.
How to approach it
Sort a copy, fix the first amount, and look for the other two with pointers from both ends of the rest. Skip repeated values in all three positions.
Sample cases
+ 3 held back until you submit
repeated lines and a credit
-100 and 300 each appear twice; every triplet must come back once, sorted ascending.
Input
Argument 1
[
300,
-100,
200,
500,
300,
0,
-100,
400
]Argument 2
600Returns
[
[
-100,
200,
500
],
[
-100,
300,
400
],
[
0,
200,
400
],
[
0,
300,
300
]
]an amount used only as often as it appears
300 + 300 + 300 and 150 + 150 + 600 need copies that are not there; only [150, 300, 450] counts.
Input
Argument 1
[
150,
300,
450,
600
]Argument 2
900Returns
[
[
150,
300,
450
]
]an invoice with no lines
No lines means no triplet: return [] without indexing into an empty list.
Input
Argument 1
[] (empty list)
Argument 2
500Returns
[] (empty list)
four zero-value lines
Four identical lines make four index triplets but only one distinct triplet, [0, 0, 0].
Input
Argument 1
[
0,
0,
0,
0
]Argument 2
0Returns
[
[
0,
0,
0
]
]Constraints
amounts_cents is a list of integers in any order. Amounts may repeat and may be negative (credits) or zero. total_cents is an integer.[300, 300, 0] needs two 300 lines.[a, b, c] with a <= b <= c and a + b + c == total_cents.a, then b, then c.[].Worked example
Take [300, -100, 200, 500, 300, 0, -100, 400] with a total of 600. Sort a copy: [-100, -100, 0, 200, 300, 300, 400, 500]. Fix the first -100; the other two must sum to 700. Two pointers on the rest: -100 + 500 = 400, too small, move left; 0 + 500, still small; 200 + 500 = 700, record [-100, 200, 500], then skip past the 200 and the 500. 300 + 400 = 700, record [-100, 300, 400]. The second -100 is the same value, so skip it entirely. Fix 0; the pair must sum to 600: 200 + 500 is too big, move right; 200 + 400, record [0, 200, 400]; then 300 + 300 (two different lines), record [0, 300, 300]. No later anchor finds a pair, so those four are the answer.
The trap: a triple loop over the original list finds [-100, 200, 500] twice, once for each -100 line, and on [0, 0, 0, 0] it reports [0, 0, 0] four times.
What this tests
Two pointers inside a loop: fix one element, then run the sorted two-pointer walk on the rest. This is LeetCode '3Sum' (15) in data clothing, and Exponent's data-engineer coding question list tags 3Sum at Adobe, Oracle, Tesla and Goldman Sachs. The target is O(n²) time after an O(n log n) sort, with O(1) extra space beyond the sorted copy and the output; the triple loop is O(n³) and repeats triplets. A data engineer meets it in reconciliation — matching a payment, a refund or a ledger adjustment against the handful of line items that explain it.
three_line_totals(amounts_cents: list[int], total_cents: int) -> list[list[int]]Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.