Out-of-Order Bank Ledger Balances
Updated
Problem statement
A bank keeps a ledger of transaction records for its accounts. Records do not arrive in time order: a record with an early time may be inserted after records with later times. Process operations in the given order. Each operation is one of:
[0, account, time, amount]: insert a record that changesaccount's balance byamount(negative for a withdrawal) attime.[1, account, time, 0]: answer the balance ofaccountattime, which is the sum of the amounts of all records for that account inserted before this query whose time is at mosttime.
An account with no such records has balance 0, and balances may be negative. Records with the same time all count. Return the answers to the queries in the order the queries appear. Balances can exceed the 32-bit integer range.
Function
processLedger(operations: int[][]) → long[]Examples
Example 1
operations = [[0,7,10,100],[0,7,30,-40],[1,7,20,0],[1,7,30,0],[0,7,5,25],[1,7,20,0],[1,9,100,0]]return = [100,60,125,0]- Account
7records+100at time10and-40at time30. - Balance of
7at time20: only the record at10counts, so100. At time30:100 - 40 = 60. - A late record
+25arrives with the earlier time5. It changes history: the balance at time20is now25 + 100 = 125. - Account
9has no records, so its balance is0.
Example 2
operations = [[0,1,50,10],[0,1,50,5],[1,1,49,0],[1,1,50,0]]return = [0,15]Two records share time 50. At time 49 neither counts (balance 0); at time 50 both count (10 + 5 = 15).
Example 3
operations = [[1,3,0,0],[0,3,0,-20],[1,3,1000000000,0]]return = [0,-20]The first query comes before any record is inserted, so it answers 0. After the withdrawal of 20 at time 0, the balance at any later time is -20; balances may go negative.
Constraints
1 <= operations.length <= 4000- Every operation has exactly four integers.
- A record is
[0, account, time, amount]with1 <= account <= 10^9,0 <= time <= 10^9and-10^6 <= amount <= 10^6. - A query is
[1, account, time, 0]with1 <= account <= 10^9and0 <= time <= 10^9. - At least one operation is a query.
More Perplexity AI interview questions
- Feature Flags with Rules, Dependencies and ExposureONSITE INTERVIEW · Seen Nov 2026
- In-Memory Filesystem CommandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Resource Reconciler: Plan and Apply CreatesPHONE SCREEN · Seen Nov 2026
- Sort Topic Names by Prefix and NumberONSITE INTERVIEW · Seen Oct 2026
- Agent Task Dependency TrackerPHONE SCREEN · Seen May 2026
- LLM Provider Pool FailoverPHONE SCREEN · Seen Aug 2025
Similar questions at other companies
- Time Based Key-Value StoreCoalition · OA
- Time Based Key-Value StoreAtlassian · OAAtlassian interview questions
- In-Memory Database with Historical Field LookupMeta · OAMeta interview questions
- Banking System with Historical SnapshotsCoinbase · OACoinbase interview questions
- Snapshot Map with Sparse Version HistorySnorkel AI · PHONE SCREEN
- Time-Based Key-Value StoreOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
- Time-Based Key-Value StoreCockroach Labs · PHONE SCREEN · ONSITE INTERVIEW
- Time Map with Sum and AverageConfluent · PHONE SCREENConfluent interview questions
- Structured Log Stream with QueriesOpenAI · PHONE SCREENOpenAI interview questions
- Time-Indexed Key-Value StoreTogether AI · ONSITE INTERVIEW