Workspace Credit Ledger
Updated
Problem statement
You keep a credit ledger for workspaces. You get a list of operations, each with a timestamp. Run them in order and return one string result for each operation.
The ledger starts empty. Timestamps are whole numbers of milliseconds, and they never go down. Operations with the same timestamp run in the order they appear in the input.
Some operations schedule a rebate, which pays credits back later. Before you run each operation from the input at timestamp t, first post every rebate that is due at or before t. Post due rebates in the order their transactions were created.
Operations
["CREATE_WORKSPACE", t, workspaceId]: create an active workspace with a balance of zero and an activity of zero. Returntrue. Returnfalseif a workspace with that ID is already active.["ADD_CREDITS", t, workspaceId, amount]: addamountcredits, a positive integer, to the workspace. Return its new balance. If the workspace is absent, return the empty string.["TRANSFER_CREDITS", t, sourceId, targetId, amount]: moveamountcredits fromsourceIdtotargetId. Both must be active, and they must be two different workspaces. On success, return the source's new balance and addamountto the source's activity. Return the empty string if either workspace is absent, the two IDs are equal, or the source does not have enough credits.["TOP_WORKSPACES", t, n]: return at mostnactive workspaces, joined by commas. Write each one asworkspaceId(activity). Put the highest activity first. Break ties by workspace ID in ascending order. A workspace's activity is the total of the credits it has successfully sent plus the credits it has successfully consumed.["CONSUME_CREDITS", t, workspaceId, amount]: takeamountcredits, a positive integer, from the workspace. Return the empty string if the workspace is absent or does not have enough credits. On success, return the next transaction ID,transactionK. HereKis one counter shared by all workspaces. It starts at one and goes up by one for each successful consumption, so failed consumptions use up no ID. A successful consumption also addsamountto the workspace's activity. It also schedules a rebate offloor(amount * 2 / 100)credits (two percent, rounded down). The rebate is due exactly86,400,000milliseconds later, which is one day.["GET_REBATE_STATUS", t, transactionId]: returnPENDINGif that transaction's rebate has not posted yet, andRECEIVEDonce it has. Return the empty string if there is no transaction with that ID.["MERGE_WORKSPACES", t, survivorId, absorbedId]: merge the absorbed workspace intosurvivorId. Both must be active, and they must be two different workspaces. Returntrueon success andfalseotherwise. The survivor gets the absorbed workspace's current balance and activity. The absorbed workspace's transactions and pending rebates now belong to the survivor. The absorbed workspace becomes inactive.["GET_BALANCE", t, workspaceId, timeAt]: ifworkspaceIdis active at timestampt, return its latest balance at or beforetimeAt. Otherwise, return the empty string. Also return the empty string iftimeAtis before the workspace was created. After a merge, the survivor keeps its own balance history from before the merge. Its combined balance starts at the merge timestamp.
If an operation that changes the ledger fails, the ledger stays exactly as it was. Return every number as a decimal string.
Function
processWorkspaceCreditOperations(operations: String[][]) → String[]Examples
Example 1
operations = [["CREATE_WORKSPACE","1","alpha"],["CREATE_WORKSPACE","2","beta"],["ADD_CREDITS","3","alpha","1000"],["TRANSFER_CREDITS","4","alpha","beta","250"],["TOP_WORKSPACES","5","2"]]return = ["true","true","1000","750","alpha(250),beta(0)"]The transfer leaves alpha with 750 credits and records 250 activity for the sender. Receiving credits does not increase beta's activity.
Example 2
operations = [["CREATE_WORKSPACE","0","a"],["ADD_CREDITS","1","a","1000"],["CONSUME_CREDITS","10","a","250"],["GET_REBATE_STATUS","86400009","transaction1"],["GET_REBATE_STATUS","86400010","transaction1"],["GET_BALANCE","86400010","a","86400010"]]return = ["true","1000","transaction1","PENDING","RECEIVED","755"]Consuming 250 credits leaves 750 and schedules a rebate of 5. One millisecond before the due time, the rebate is still pending. At the due time, the rebate posts before that operation runs, so the status is received and the balance query already includes the rebate.
Example 3
operations = [["CREATE_WORKSPACE","1","x"],["CREATE_WORKSPACE","2","y"],["ADD_CREDITS","3","x","100"],["ADD_CREDITS","4","y","200"],["CONSUME_CREDITS","5","y","50"],["MERGE_WORKSPACES","6","x","y"],["GET_BALANCE","7","x","4"],["GET_BALANCE","8","x","6"],["GET_REBATE_STATUS","86400005","transaction1"],["GET_BALANCE","86400005","x","86400005"],["GET_BALANCE","86400006","y","5"]]return = ["true","true","100","200","transaction1","true","100","250","RECEIVED","251",""]Before the merge, at timestamp 4, x has its own balance of 100. The merge records the combined balance 250 at timestamp 6. The pending rebate from y moves to the survivor and raises x's balance to 251. y is now inactive, so querying it returns the empty string.
Constraints
1 <= operations.length <= 2000.- Every timestamp is a nonnegative integer that fits in a signed 64-bit integer. Timestamps never go down. Every rebate due time also fits in a signed 64-bit integer.
- Every operation has one of the valid shapes listed above.
- Workspace IDs are non-empty strings of printable ASCII characters. They contain no commas or parentheses.
- Every credit amount is a positive integer. All balances and activity totals fit in signed 64-bit integers.
1 <= n <= 10^5forTOP_WORKSPACES.0 <= timeAt <= tforGET_BALANCE.
More Airbnb Online Assessment questions
- Bouncing DiagonalsOA · Seen Dec 2026
- Count Numbers Not Equal to the First TwoOA · Seen Dec 2026
- Garden Sprinkler PlacementOA · Seen Dec 2026
- Theme Park Shuttle Wait TimeOA · Seen Dec 2026
- Count Card Duel RoundsOA · Seen Nov 2026
- Count Lamps Lighting Each PointOA · Seen Nov 2026
- Simulate Orchard Rot SpreadOA · Seen Nov 2026
- Sort Adjacent PairsOA · Seen Nov 2026
- In-Memory File SystemOA · Seen Nov 2026
- Task Management SystemOA · Seen Nov 2026
Similar questions at other companies
- Cloud Compute Credits SystemCoinbase · OACoinbase interview questions
- Document Enrichment Stream AggregatorBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Power Cell BankOptiver · OAOptiver interview questions
- Four-Level Banking System with Scheduled PaymentsZipRecruiter · OAZipRecruiter interview questions
- Weighted LFU CacheIMC Trading · OAIMC Trading interview questions
- Weighted LFU CacheMicrosoft · OAMicrosoft interview questions
- Task Management SystemAnthropic · OAAnthropic interview questions
- Progressive Parcel Tracking OperationsKlaviyo · OAKlaviyo interview questions
- Versioned Recipe SystemThe Trade Desk · OA
- Recipe Management SystemRamp · OARamp interview questions