KatInterview

Out-of-Order Loan Event Ledger with Fee-First Payments

Data Structure DesignAuto-graded · 19 test cases
MediumAuto-gradedData Structure Designsimulationsorted-insertdata-structure-design

Last updated

Tests ordered insertion, fee-before-principal payment allocation, and efficient forward-propagation of balance changes when events arrive out of sequence.

Out-of-Order Loan Event Ledger with Fee-First Payments

You are building a simplified loan accounting system that tracks a loan's balance history as events stream in — potentially out of chronological order.

Loan State

A loan begins with:

  • Principal balance: 100000
  • Fee balance: 0

Event Types

Each event is described by three values: an event type ("PAYMENT" or "FEE"), a positive integer amount, and an integer accounting_sequence that defines where the event belongs in the official ledger order.

  • A "FEE" event increases the fee balance by the given amount.
  • A "PAYMENT" event first reduces the fee balance as much as possible, then applies any remainder to the principal balance.

Task

Given a list of events, process them one at a time in the order they appear in the input list. After each event is processed, produce a snapshot of the full ledger history sorted by accounting_sequence.

Each entry in the history is a three-element list [accounting_sequence, principal_balance, fee_balance] representing the loan state immediately after that event's effect.

Return a list of these snapshots — one snapshot per event, in input order.

Key Details

  • Events may arrive with an accounting_sequence that places them before already-processed events. When this happens, all subsequent ledger entries must be recomputed to reflect the insertion.
  • When two events share the same accounting_sequence, the one received later in the input is placed after the earlier-received one (stable / append-to-right tie-breaking via bisect_right).
  • self.balances (if you model this as a class) should always reflect the state at the end of the highest-sequence entry.
  • Balances may go negative if a payment exceeds the remaining principal after fees are cleared.

Constraints

  • 1 <= len(events) <= 1000
  • Each event is [event_type, amount, accounting_sequence]
  • event_type is either "PAYMENT" or "FEE"
  • 1 <= amount <= 10^9
  • 1 <= accounting_sequence <= 10^9
  • All values are integers except event_type

Examples

Example 1

Input: process_loan_events([['PAYMENT', 1000, 1], ['PAYMENT', 500, 3], ['FEE', 100, 2]])

Output: [[[1, 99000, 0]], [[1, 99000, 0], [3, 98500, 0]], [[1, 99000, 0], [2, 99000, 100], [3, 98600, 0]]]

Example 2

Input: process_loan_events([['FEE', 100, 2], ['PAYMENT', 1000, 1]])

Output: [[[2, 100000, 100]], [[1, 99000, 0], [2, 99000, 100]]]

Hints

Hint 1

Use binary search on a sorted list of accounting sequences to find the correct insertion index for each incoming event. Python's bisect module makes this straightforward.

Hint 2

Once you insert the new event at index i, you only need to recompute balances for entries at index i and beyond — entries before i are unaffected. Start the forward pass from the state stored at index i-1 (or the initial loan state if i is 0).

Hint 3

After the forward pass, the final entry in the sorted list holds the loan state at the highest accounting sequence. That is the value to store as the current balances.