ORIGINAL CODING QUESTION
Undo and redo in a notes editor
A notes editor holds a sequence of numbers and processes a log. (1, x) appends x to the end. (2, 0) undoes the most recent append that is still in effect, removing that number. (3, 0) redoes the most recently undone append, putting the number back at the end. Any new append clears the redo history, and an undo or redo with nothing available does nothing. Return the sum over the final sequence of each number times its 1-based position.
Original exercise · medium. Editorial role relevance does not imply a company has asked this question.
Input and output
q, then q operation pairs (type value); undo and redo carry value 0. Print the weighted sum.
Constraints
1 ≤ q ≤ 200; for type 1, -1000 ≤ x ≤ 1000.
Example 1
Input
5 1 4 1 5 2 0 1 6 3 0
Output
16
Undo removes 5, appending 6 clears the redo history, and the redo does nothing: 1*4 + 2*6 = 16.
Example 2
Input
6 1 3 1 7 2 0 2 0 3 0 3 0
Output
17
Both appends are undone and then redone in order, restoring 3, 7: 3 + 14 = 17.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
Use one stack for the document and another for undone numbers.
Hint 2
Undo moves the document top onto the redo stack; redo moves it back.
Hint 3
Clear the redo stack on every append, then compute the weighted sum of the document at the end.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →