Ctrl K

Quiz Score Log

Easy
A quiz host keeps a team's scores in a log that starts out empty. The entries entries come in order, and each one is one of: - an integer x: add the score x to the log; - "+": add a score equal to the sum of the last two scores in the log; - "D": add twice the last score; - "C": cancel the last score and strike it from the log. Return the sum of all scores left in the log after every entry is processed. The entries are always valid: before "+" the log has at least two scores, and before "D" or "C" it has at least one.
Example 1
entries=["4", "7", "+", "C", "D"] → 25

The log goes [4] → [4, 7] → [4, 7, 11] → "C" removes 11: [4, 7] → "D" adds 14: [4, 7, 14]. The total is 25.

Example 2
entries=["-3", "D", "8", "+"] → 1

[-3] → [-3, -6] → [-3, -6, 8] → [-3, -6, 8, 2]. The total is 1.

Example 3
entries=["9", "C"] → 0

The only score is cancelled, the log is empty, so the total is 0.

Constraints
  • 1 ≤ entries.length ≤ 1000
  • entries[i] is "+", "D", "C", or an integer from -3·10⁴ to 3·10⁴
  • Every intermediate score and the answer are below 2³¹ in absolute value
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsA Picky Line at the Counter EasyRobots Clashing on a Rail MediumReplaying Stack Pushes and Pops MediumA Stack From a Number Stream MediumRobots Crashing on a Rail Hard
loading Python…
Input
entries=["4", "7", "+", "C", "D"]
Expected
25

Run checks the first 3 tests in your browser. Submit checks all 74 tests on the server, hidden ones included.