Quant Memo
Coding/●●●●●

Two order sizes closest to a target

You want to combine two orders whose sizes add up as close as possible to a target notional T. Given a list of sizes, return the pair whose sum is nearest to T (in absolute difference).

sizes = [1, 3, 4, 7], T = 6
-> (1, 4)          # sum 5 is 1 away; ties resolved by first found

The sums 1 + 4 = 5 and 3 + 4 = 7 are each 1 away from 6; this solution returns the first one it encounters, (1, 4).

Return a pair (x, y) minimizing |x + y - T|. Aim for O(nlogn)O(n \log n).

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions