KatInterview

Chunked Stream Parser and Debt Settlement

StringsAuto-graded · 22 test cases
MediumAuto-gradedStringsstringsgreedy-intervalsdesign-ds

Last updated

Tests buffered string parsing over an incremental data source combined with greedy net-balance clearing across a set of financial transfers.

Part 1: Reconstruct Lines from a Chunked Text Stream

A text source delivers data in arbitrary-sized chunks. Each chunk is a plain string that may contain zero or more newline characters (\n). When all chunks are joined together, the result is a sequence of logical lines separated by \n.

You receive the chunks as a list chunks. Reconstruct every logical line and return them in order as a list of strings. Do not include the newline characters themselves in the returned strings.

Rules for edge cases:

  • An empty chunk has no effect.
  • Two consecutive newlines produce an empty line between them.
  • If the concatenated text ends with a newline, do not append a phantom empty line after it.
  • If the text does not end with a newline, the remaining characters after the last newline form the final line.
  • If chunks is empty, return an empty list.

You must process chunks one at a time and carry only the unfinished tail of the current line across iterations (O(L_max) extra memory, where L_max is the length of the longest single line).


Part 2: Settle Debts with Payback Transfers

You are given a list of money transfers transactions, where each entry [sender, receiver, amount] means sender paid receiver exactly amount units.

Compute each person's net balance:

  • Every amount sent reduces the sender's balance.
  • Every amount received increases the receiver's balance.

Then produce a list of payback transfers [payer, recipient, amount] such that, after applying the paybacks on top of the original transactions, every person's net balance is exactly zero. You do not need to minimize the number of payback transfers; any valid set is acceptable as long as it fully clears all balances.

Rules:

  • Use a greedy two-pointer approach: match the current net-debtor (negative balance) against the current net-creditor (positive balance), transfer min(|debtor balance|, creditor balance), and advance whichever pointer reaches zero (advance both if they reach zero simultaneously).
  • Build the debtor list and creditor list in the order people first appear in transactions (dictionary insertion order).
  • Use integers only; do not introduce floating-point arithmetic.
  • If all balances are already zero (including when transactions is empty), return an empty list for the paybacks.

Return value

Return a list of exactly two elements:

[lines, paybacks]
  • lines — the list of reconstructed strings from Part 1.
  • paybacks — the list of [payer, recipient, amount] payback transfers from Part 2.

Constraints

  • 0 ≤ len(chunks) ≤ 10^4
  • Each chunk is a string of printable ASCII characters and/or \n; total characters across all chunks ≤ 10^6.
  • 0 ≤ len(transactions) ≤ 10^4
  • Each transaction is [sender, receiver, amount] where sender and receiver are non-empty strings and 1 ≤ amount ≤ 10^9.
  • The input guarantees that all balances are integers and that total debts equal total credits.

Examples

Example 1

Input: parse_chunks_and_settle(['one\\ntw', 'o\\n', 'three\\nfour', '\\nfi', 've'], [['A', 'B', 10], ['B', 'C', 5], ['A', 'C', 5]])

Output: [['one\\ntwo\\nthree\\nfour\\nfive'], [['A', 'B', 5], ['A', 'C', 10]]]

Example 2

Input: parse_chunks_and_settle(['alpha\\n', 'beta'], [])

Output: [['alpha\\nbeta'], []]

Hints

Hint 1

For Part 1, keep a single string variable that holds the characters after the last newline seen so far. When you process a new chunk, prepend that variable to the chunk and split on newline — everything except the last piece is a complete line.

Hint 2

For Part 2, individual transfers don't matter — only each person's final net position does. Compute one balance per person, then separate everyone into two groups: those who owe money and those who are owed money.

Hint 3

For the greedy matching in Part 2, use two pointers, one into each group. At each step, move min(debt, credit) from the debtor to the creditor, reduce both by that amount, and advance the pointer that hit zero. Because total debt always equals total credit, both pointers will reach the end at the same time.