Container Ledger with Trades
Updated
Problem statement
Given a list of operations on a container ledger, run them in order and return an array with one integer result per operation.
The ledger records how many shipping containers each customer holds. This number is the customer's balance. Every customer starts with a balance of 0, including a customer who appears for the first time in any operation.
Each string in operations has one of these three forms, with parts separated by single spaces. <customer>, <seller> and <buyer> are customer names, and <count> is a positive whole number.
"ADD <customer> <count>":<customer>receives<count>new containers. The result is the customer's balance after the addition."TRADE <seller> <buyer> <count>":<seller>gives<count>containers to<buyer>. If the seller's balance is at least<count>, the seller's balance goes down by<count>, the buyer's goes up by<count>, and the result is1. Otherwise the trade is refused: no balance changes, not even in part, and the result is0."BALANCE <customer>": the result is the customer's current balance, which is0for a customer not seen before.
Every result includes the effect of all earlier operations.
Function
containerLedger(operations: String[]) → int[]Examples
Example 1
operations = ["ADD acme 10","ADD nova 4","TRADE acme nova 3","ADD nova 2","BALANCE acme","BALANCE nova"]return = [10,4,1,9,7,9]The two ADD operations give acme 10 containers and nova 4. The trade moves 3 from acme to nova, so each has 7. Adding 2 more gives nova 9.
Example 2
operations = ["ADD acme 5","TRADE acme nova 6","BALANCE acme","TRADE acme nova 5","BALANCE acme","BALANCE nova"]return = [5,0,5,1,0,5]acme has only 5 containers, so the trade of 6 is refused and nothing moves, not even part of it. A trade of exactly 5 goes through and leaves acme with 0.
Example 3
operations = ["BALANCE zenith","TRADE zenith acme 1","ADD acme 2","TRADE acme zenith 2","TRADE zenith nova 1","ADD acme 3","BALANCE zenith","BALANCE nova","BALANCE acme"]return = [0,0,2,1,1,3,1,1,3]zenith has not appeared before, so its balance is 0 and its trade is refused. After it receives 2 containers from acme, it can trade 1 of them to nova, a customer seen for the first time.
Constraints
1 <= operations.length <= 1000- Each
operations[i]has one of the three forms above, with single spaces between its parts. - Each customer name has
1to10characters, each a lowercase letter or a digit. - In each
TRADE,<seller>and<buyer>are different customers. - Each
<count>is a whole number from1to10^6, written without leading zeros. - Every balance stays at most
10^9.
More Flexport interview questions
- Choose the Next Hidden-Word GuessONSITE INTERVIEW · Seen Jul 2026
- Apply Operations to Make a String EmptyPHONE SCREEN · Seen Nov 2026
- Max Area of Island Without Changing the GridPHONE SCREEN · Seen Nov 2026
- Count Good SubsequencesOA · Seen Jun 2024
- Distinct MovesOA · Seen Jun 2024
- Hotspot ConnectionsOA · Seen Jun 2024
- Throw The BallOA · Seen Jun 2024
- Matrix TraversalOA · Seen Feb 2024
- Collect PointsOA · Seen Feb 2024
- Malware SpreadOA · Seen Feb 2024
Similar questions at other companies
- Account Balance Manager Part 2 - Reject OverdraftsStripe · ONSITE INTERVIEWStripe interview questions
- Card Transaction StreamCapital One · ONSITE INTERVIEWCapital One interview questions
- Route Pattern MatcherGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions
- Feature Flags with Rules, Dependencies and ExposurePerplexity AI · ONSITE INTERVIEWPerplexity AI interview questions
- Generate Word Guess FeedbackAtlassian · ONSITE INTERVIEWAtlassian interview questions
- Stable Unique Column RenamesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Most Frequent Call Path in Interleaved LogsRoblox · ONSITE INTERVIEWRoblox interview questions
- Dictionary Records to an Editable XML TreeSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions