FastPrepDriver Balance Ledger

Driver Balance Ledger

Rippling logoRippling● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Maintain an in-memory balance ledger for delivery drivers. Process each operation in order:

  • ["ADJUST", driverId, delta]: add the signed integer delta to the driver's current balance. A driver has balance 0 before the first adjustment.
  • ["BALANCE", driverId]: append the driver's current balance to the result. An unseen driver has balance 0.
  • ["TOTAL"]: append the sum of every driver's current balance to the result.

Only BALANCE and TOTAL operations produce output. Return those values in operation order.

The total-balance operation may be called very frequently, so it should not scan every driver on each call.

Function

processDriverBalances(operations: String[][]) → long[]

Examples

Example 1

operations = [["ADJUST","alice","1200"],["ADJUST","bob","800"],["TOTAL"],["BALANCE","alice"],["ADJUST","alice","-300"],["TOTAL"]]return = [2000,1200,1700]

After the first two adjustments, the total is 1200 + 800 = 2000. Alice's balance is 1200. Her final adjustment lowers both her balance and the cached total by 300, so the last result is 1700.

Example 2

operations = [["BALANCE","d7"],["TOTAL"],["ADJUST","d7","-50"],["BALANCE","d7"],["TOTAL"]]return = [0,0,-50,-50]

An unseen driver starts at 0, and an empty ledger also totals 0. Signed adjustments may make a balance and the overall total negative.

Example 3

operations = [["ADJUST","a","5"],["ADJUST","a","-5"],["ADJUST","b","9"],["TOTAL"],["BALANCE","a"],["BALANCE","b"]]return = [9,0,9]

Repeated adjustments accumulate for the same driver. Driver a returns to 0, while driver b contributes the entire total of 9.

Constraints

  • 1 <= operations.length <= 2 * 10^5.
  • Each operation has one of the documented forms.
  • Each driverId contains 1 to 40 ASCII letters, digits, hyphens, or underscores.
  • Each delta is a decimal integer string between -10^9 and 10^9, inclusive.
  • Every individual driver balance and the total balance fit in a signed 64-bit integer.

More Rippling problems

See Rippling hiring insights
public long[] processDriverBalances(String[][] operations) {
    // Write your code here.
}
operations[["ADJUST","alice","1200"],["ADJUST","bob","800"],["TOTAL"],["BALANCE","alice"],["ADJUST","alice","-300"],["TOTAL"]]
expected[2000,1200,1700]
Checking account…