Problem · Array

Billing Log with Undo and Redo

Learn this problem
MediumReddit logoRedditFULLTIMEPHONE SCREEN

Problem statement

A billing amount starts at 0. Process paired arrays operations and amounts in order:

  • ADD adds the paired amount.
  • SET overwrites the current amount with the paired amount.
  • UNDO reverses the most recently applied ADD or SET, if one exists.
  • REDO reapplies the most recently undone change, if one exists.

A new ADD or SET after an undo discards the redo history. Amounts paired with UNDO and REDO are ignored. Return the current amount after every operation.

Function

billingStatusLog(operations: String[], amounts: long[]) → long[]

Examples

Example 1

operations = ["ADD","ADD","UNDO","REDO"]amounts = [5,3,0,0]return = [5,8,5,8]

Undo removes the second addition and redo reapplies it.

Example 2

operations = ["SET","ADD","UNDO","SET","REDO"]amounts = [10,5,0,7,0]return = [10,15,10,7,7]

The new SET after undo clears the redo history, so the final redo is a no-op.

Constraints

  • 1 <= operations.length == amounts.length <= 100000
  • Every operation is ADD, SET, UNDO, or REDO.
  • -1000000000 <= amounts[i] <= 1000000000
  • Every intermediate amount fits in a signed 64-bit integer.

More Reddit problems

drafts saved locally
public long[] billingStatusLog(String[] operations, long[] amounts) {
    // Write your code here
}
operations["ADD","ADD","UNDO","REDO"]
amounts[5,3,0,0]
expected[5,8,5,8]
checking account