Bank Accounts With Undo
Updated
Problem statement
A bank keeps one balance per account. There is no account setup: an account that has never been used holds 0. Run the entries of commands in order. Each one is:
"DEPOSIT id amount": addamountto the account and return"OK"."WITHDRAW id amount": if the balance is at leastamount, take it out and return"OK". Otherwise return"FAILED"and change nothing."TRANSFER from to amount": iffromandtoare different accounts andfromholds at leastamount, moveamountfromfromtotoand return"OK". Otherwise return"FAILED"and change nothing."UNDO": reverse the most recentDEPOSIT,WITHDRAWorTRANSFERthat succeeded and has not been reversed yet, and return"OK". If there is none, return"NOTHING". A command that returned"FAILED"is never reversed, and neither is anUNDO."BALANCE id": return the balance of the account as a decimal string. It changes nothing and is never reversed.
Return one string per command, in the order of commands.
Function
runBankCommands(commands: String[]) → String[]Examples
Example 1
commands = ["DEPOSIT ann 100","WITHDRAW ann 30","WITHDRAW ann 500","TRANSFER ann bob 50","BALANCE ann","BALANCE bob","UNDO","BALANCE ann","BALANCE bob","UNDO","BALANCE ann","UNDO","BALANCE ann"]return = ["OK","OK","FAILED","OK","20","50","OK","70","0","OK","100","OK","0"]After the first four commands ann holds 20 and bob holds 50; the withdrawal of 500 fails and is not remembered. The first UNDO reverses the transfer and restores both balances (70 and 0). The next UNDO reverses the withdrawal of 30, not the failed one, so ann is back to 100; the last one removes the deposit and leaves 0. BALANCE commands are never reversed.
Example 2
commands = ["UNDO","DEPOSIT a 10","UNDO","UNDO","BALANCE a","DEPOSIT a 5","WITHDRAW a 9","TRANSFER a a 1","UNDO","BALANCE a","UNDO","UNDO","BALANCE a"]return = ["NOTHING","OK","OK","NOTHING","0","OK","FAILED","FAILED","OK","0","NOTHING","NOTHING","0"]The first UNDO has nothing to reverse. After DEPOSIT a 10, an UNDO takes it back, and the UNDO after that finds nothing: an undo is not a step that can itself be reversed or repeated, so the balance stays 0. Later the withdrawal of 9 is refused (only 5 is there) and so is the transfer of an account to itself. Neither is remembered, so the next UNDO reverses the deposit of 5, and the two after it find nothing.
Example 3
commands = ["DEPOSIT x 40","TRANSFER x y 40","WITHDRAW y 40","BALANCE x","BALANCE y","UNDO","BALANCE y","UNDO","BALANCE x","BALANCE y","TRANSFER y x 41","BALANCE y"]return = ["OK","OK","OK","0","0","OK","40","OK","40","0","FAILED","0"]The transfer moves all 40 from x to y, which is allowed (a balance may reach 0), and y did not exist before it. Withdrawing 40 from y is allowed too, so both balances are 0. The first UNDO gives y its 40 back; the second reverses the transfer, so x holds 40 again and y holds 0. The transfer of 41 from y fails.
Constraints
1 <= commands.length <= 2000- Each command has one of the five forms above, with single spaces, and is 4 to 28 characters long.
- Every account
idis 1 to 6 lowercase letters or digits, and everyamountis a whole number from1to10^4.
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- Code Editor FoldingJane Street · ONSITE INTERVIEWJane Street interview questions
- Undo and Redo Command HistoryNetflix · ONSITE INTERVIEWNetflix interview questions
- Text Document HistoryNotion · PHONE SCREEN · ONSITE INTERVIEWNotion interview questions
- Transactional In-Memory RedisHashiCorp · ONSITE INTERVIEWHashiCorp interview questions
- Key-Value Store with Nested TransactionsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Implement a Queue Using Two StacksAmazon · ONSITE INTERVIEW
- Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions
- Multiline Text Editor with Cursor MovementDatologyAI · PHONE SCREEN
- Layer History with Batch Undo and RedoFigma · OA · PHONE SCREENFigma interview questions
- Undo/Redo Timeline State EngineKickdrum · OA
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences