Chunked Stream Reader II
You are given an API you cannot modify:
readChunk() -> string // next chunk of a log stream; "" means end of stream
The judge implements readChunk and passes it into your code: it is the argument settleFromStream receives (a plain callback in JavaScript, TypeScript, and Python; a Supplier<String> in Java; a function<string()> in C++; a func() string in Go). Don't define it yourself, and don't call it as a global — use the one handed to you.
Each line of the stream is payer,payee,amount with an integer amount: the payer paid that amount on the payee's behalf. Chunks split arbitrarily — one chunk may hold several lines, and one line may span several chunks — and the final line may lack a trailing newline. Blank lines carry nothing.
Implement settleFromStream(readChunk): parse the stream, compute each person's net balance, and return the minimum number of transactions needed to settle everyone.
chunks: ["a,b,5\na,c", ",5\nb,c,5"] lines a,b,5 a,c,5 b,c,5 => 1 chunks: ["a,b,1\nc,d,1"] => 2
Hints
Hint 1
Reassembling lines is its own job: keep the fragments of the current unterminated line, and every newline in a chunk completes one. Flush the last fragment when the stream ends.
Hint 2
Only net balances matter — people who net to zero drop out entirely.
Hint 3
Settling the remaining balances in the fewest transactions is a backtracking search: match each nonzero balance against later opposite-sign ones, skipping values already tried at the same depth.