Add and Subtract With Undo and Redo
Updated
Problem statement
A calculator keeps a running total that starts at 0. It supports four operations:
add v: addvto the total.subtract v: subtractvfrom the total.undo: take back the latest add or subtract that has not been taken back yet. If there is none, nothing happens.redo: bring back the step that was taken back most recently. If there is none, nothing happens.
A new add or subtract ends the redo history: steps undone before it can no longer be redone. A step that was redone can be undone again.
Operation i is operations[i] with value values[i]. Return the total after each operation, in order. The total can be negative and can be larger than a 32-bit integer.
Function
runCalculator(operations: String[], values: int[]) → long[]Examples
Example 1
operations = ["add","add","undo","subtract","redo","undo","undo"]values = [5,3,0,2,0,0,0]return = [5,8,5,3,3,5,0]Totals: add 5 gives 5, add 3 gives 8, undo takes back the 3 (5). Subtract 2 gives 3 and also wipes out the undone add 3, so redo has nothing to redo (3). The two undos take back the subtract (5) and then the add 5 (0).
Example 2
operations = ["undo","redo","add"]values = [0,0,4]return = [0,0,4]With nothing done yet, undo and redo change nothing and the total stays 0. Then add 4 gives 4.
Example 3
operations = ["add","subtract","undo","undo","redo","redo","redo"]values = [10,4,0,0,0,0,0]return = [10,6,10,0,10,6,6]10, then 6. Two undos go back to 10 and then 0. Two redos bring back the add (10) and the subtract (6). The third redo has nothing left to redo, so the total stays 6.
Constraints
1 <= operations.length <= 2000andvalues.length == operations.length- Each
operations[i]is"add","subtract","undo"or"redo" 0 <= values[i] <= 10^9for"add"and"subtract";values[i] == 0for"undo"and"redo"
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Implement a Queue Using Two StacksOracle · PHONE SCREENOracle interview questions
- 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
- Stack with Constant-Time Middle QueriesAmazon · PHONE SCREEN
- Nested Recipe Cart TransactionsDecagon · PHONE SCREENDecagon interview questions
- Text Editor, Part 2: Undo and RedoOpenAI · PHONE SCREENOpenAI interview questions
- Text Document HistoryNotion · PHONE SCREEN · ONSITE INTERVIEWNotion interview questions
- Key-Value Store with Nested TransactionsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Code Editor FoldingJane Street · ONSITE INTERVIEWJane Street interview questions