Baseball Game

EasyStacksimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are keeping score for a game with unusual rules. Scoring starts with an empty record, and you process the strings in operations one by one: - An integer string "x" records a new score of x (it may be negative). - "+" records a new score equal to the sum of the two most recent scores. - "D" records a new score equal to double the most recent score. - "C" cancels the most recent score, removing it from the record. Every operation is guaranteed to be valid (for example, "+" always has two scores available). Return the sum of all scores left in the record after all operations (0 if the record is empty).

Examples

Input: operations = ["5","2","C","D","+"]
Output: 30
Record goes [5] → [5,2] → [5] → [5,10] → [5,10,15]; the sum is 30.
Input: operations = ["5","-2","4","C","D","9","+","+"]
Output: 27
Final record is [5,-2,-4,9,5,14], which sums to 27.
Input: operations = ["1","C"]
Output: 0
The only score is cancelled, leaving an empty record.

Constraints

  • 1 <= operations.length <= 1000
  • Integer operations are in the range [-3 * 10^4, 3 * 10^4].
  • All intermediate scores fit in a 32-bit signed integer.
  • Every operation is valid for the current record.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems