This question evaluates knowledge of data structures and algorithms for transactional ledgers, including tracking payments and refundable balances, rule-based prioritization for allocating refunds, handling partial and multiple refunds, and ensuring correctness when payments and refunds interleave.
Design and implement a transaction ledger that ingests a stream of records where each record is either a payment or a refund. Each payment has {id, userId, amount, timestamp}; each refund has {id, userId, amount, timestamp, optional paymentIdReference}. (