Answer As-Of Balance Queries Over an Immutable Ledger
Last updated: November 29, 2026
Quick Overview
Given an append-only list of ledger entries (transaction time, account, signed amount in cents) in any order, build a structure that answers balance_as_of(account, t), the account balance including all entries with transaction time at or before t.
Nubank
November 29, 20260
0
0 solved
Given an append-only list of ledger entries (transaction time, account, signed amount in cents) in any order, build a structure that answers balance_as_of(account, t), the account balance including all entries with transaction time at or before t.
Nubank runs much of its core on Datomic, where facts are never updated in place and any past state of the database can be queried. This exercise asks you to reproduce that as-of idea for balances, and the interviewer checks preprocessing plus binary search, and whether you can talk about bitemporal corrections.
What the Interviewer Expects
- Groups entries by account and sorts by transaction time
- Builds prefix sums so each query is a binary search
- Uses bisect_right so entries exactly at t are included
- Returns 0 for unknown accounts or times before the first entry
- Discusses the difference between transaction time and business (valid) time
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Entries keep arriving while queries run. How do you support appends efficiently?
- A reversal entry corrects a purchase from last month. Should the as-of balance for last month change?
- How would you answer range sums (net flow between t1 and t2)?
- How would this map onto Datomic's as-of and history views?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Preprocess once, then many queries. Entries can be unsorted. Balance as of t includes every entry with tx_time <= t. Money is in integer cents....
High-Level Design
Per account, sort entries by time and store two parallel arrays: times and cumulative sums. A query finds how many entries are at or before t with bisect_right and returns the prefix sum at that i...