Bank Accounts with Balance History
Updated
Problem statement
Build a small bank that also remembers its past. You are given a list of operations in the order they happen. Each operation is a string array whose first two fields are the operation's name and its timestamp t.
["CREATE", t, account]opens a new account with balance0and returns1. If the account already exists, nothing changes and it returns0.["DEPOSIT", t, account, amount]addsamountto the account and returns its new balance. It returns-1if the account does not exist.["TRANSFER", t, from, to, amount]movesamountfrom accountfromto a different accounttoand returns the new balance offrom. It returns-1if either account does not exist, iffromandtoare the same account, or iffromholds less thanamount.["BALANCE_AT", t, account, time]returns the balance thataccounthad at timetime, that is, after every operation with a timestamp up to and includingtime. It returns-1if the account did not exist at that time. This operation changes nothing.
A rejected operation changes no balance. Return an array with one result for each operation, in the same order.
Function
processBankOperations(operations: String[][]) → long[]Examples
Example 1
operations = [["CREATE","1","alice"],["CREATE","2","bob"],["DEPOSIT","3","alice","500"],["TRANSFER","5","alice","bob","200"],["BALANCE_AT","6","alice","4"],["BALANCE_AT","7","bob","6"],["BALANCE_AT","8","bob","1"]]return = [1,1,500,300,500,200,-1]- Both accounts are created (
1,1). The deposit givesalicea balance of500, and the transfer at time5leaves her300. - At time
4the transfer had not happened yet, soalicehad500. At time6,bobhad the200he received. - At time
1,bobdid not exist yet (he was created at time2), so the answer is-1.
Example 2
operations = [["CREATE","10","acct1"],["CREATE","20","acct1"],["DEPOSIT","30","acct2","50"],["DEPOSIT","40","acct1","50"],["TRANSFER","50","acct1","acct1","10"],["CREATE","60","acct2"],["TRANSFER","70","acct1","acct2","80"],["TRANSFER","80","acct1","acct3","10"],["BALANCE_AT","90","acct1","85"],["BALANCE_AT","95","acct3","90"]]return = [1,0,-1,50,-1,1,-1,-1,50,-1]- Creating
acct1a second time returns0, and a deposit intoacct2before it exists returns-1. - A transfer from
acct1to itself, a transfer of80whenacct1holds only50, and a transfer to the unknownacct3are all rejected with-1. - Rejected operations change nothing, so
acct1still had50at time85.acct3never existed, so its answer is-1.
Example 3
operations = [["CREATE","100","x"],["DEPOSIT","200","x","7"],["DEPOSIT","300","x","3"],["BALANCE_AT","400","x","200"],["BALANCE_AT","500","x","199"],["BALANCE_AT","600","x","100"],["BALANCE_AT","700","x","99"],["BALANCE_AT","800","x","800"]]return = [1,7,10,7,0,0,-1,10]- A change made exactly at the asked time counts: at time
200the balance was7, but at time199it was still0. - The account exists from time
100on, so time99gives-1. Asking at the query's own time,800, gives the current balance10.
Constraints
1 <= operations.length <= 1000- Each operation has exactly the fields shown above, and its name is one of
CREATE,DEPOSIT,TRANSFERandBALANCE_AT. - Timestamps are integers from
1to10^9, strictly increasing from one operation to the next. - In a
BALANCE_AToperation,1 <= time <= t. - Account names have 1 to 10 characters, each a lowercase letter or a digit.
- Every
amountis an integer from1to10^9. - All numbers are written as decimal strings without leading zeros.
More Meta interview questions
- In-Memory Database with Historical Field LookupOA · Seen Sep 2026
- Cave AdvantureOA · Seen May 2024
- Closest Land Mark 👏OA · Seen Feb 2024
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockPHONE SCREEN · Seen Nov 2026
- First Element Greater Than KPHONE SCREEN · Seen Nov 2026
- Count Events in Rolling Minute WindowsPHONE SCREEN · Seen Oct 2026
- Valid Word AbbreviationPHONE SCREEN · Seen Oct 2026
- Shortest Path in a Binary MatrixPHONE SCREEN · Seen Sep 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Process Banking OperationsVercel · OA
- Parcel Attribute Store with TTLAirbnb · OAAirbnb interview questions
- Banking System with Historical SnapshotsCoinbase · OACoinbase interview questions
- Consistent-Hash Packet RouterAmazon · PHONE SCREEN
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- Process On-Call Routing OperationsOpenAI · PHONE SCREENOpenAI interview questions
- Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
- Escape Room LeaderboardPinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Role-Based Network Access ControlOkta · PHONE SCREENOkta interview questions
- Future-Effective Employee SalaryRippling · ONSITE INTERVIEWRippling interview questions