ORIGINAL CODING QUESTION
Lowest score on a review stack
A reviewer keeps a stack of score cards and processes a log of operations. (1, x) pushes a card with score x. (2, 0) removes the top card if the stack is not empty and otherwise does nothing. (3, 0) asks for the lowest score currently on the stack. Return the sum of the answers to all lowest-score questions, where a question asked on an empty stack contributes 0. Aim for constant time per operation.
Original exercise · easy. Editorial role relevance does not imply a company has asked this question.
Input and output
q, then q operation pairs (type value). Print the sum of query answers.
Constraints
1 ≤ q ≤ 200; for type 1, -1000 ≤ x ≤ 1000; types 2 and 3 always carry value 0.
Example 1
Input
6 1 5 1 3 3 0 2 0 3 0 1 7
Output
8
The questions see minimums 3 and then 5, totalling 8.
Example 2
Input
4 3 0 2 0 1 -4 3 0
Output
-4
The first question is on an empty stack (0); the second sees -4.
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
Scanning the whole stack for every question is quadratic in the worst case.
Hint 2
Alongside each card, remember the lowest score at or below it in the stack.
Hint 3
Push min(x, current minimum) onto a second stack, pop both stacks together, and read the top of the minimum stack for each question.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →