Replay a Moderation Score Log With Undo and Double Operations

Last updated: November 29, 2026

Quick Overview

A moderator's score log contains integers plus three commands: "+" adds the last two valid scores, "D" doubles the last valid score, and "C" cancels the last valid score. Return the sum of all valid scores after replaying the log.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


A moderator's score log contains integers plus three commands: "+" adds the last two valid scores, "D" doubles the last valid score, and "C" cancels the last valid score. Return the sum of all valid scores after replaying the log.

A simulation question that fits Meta online assessments, framed around an integrity tooling log. The interviewer wants a direct stack-based replay and clear handling of each command's preconditions.

What the Interviewer Expects
  • Choose a stack because commands refer to the most recent valid entries
  • Handle each command in O(1)
  • Parse negative integers correctly
  • Clarify what happens on invalid commands
  • Return the final sum
Key Topics to Cover
Stack
Simulation
Command parsing
Input validation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you support redo after a cancel?
  • What if the log is too big for memory but you only need the final sum?
  • How would you validate a malformed log and report the first bad line?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: list of strings, each an integer (possibly negative), "+", "D" or "C". Guarantee to confirm: "+" always has two prior valid scores, "D" and "C" at least one. Output: integer sum of the remainin...

High-Level Design

Every command acts on the most recent valid scores, and "C" removes the most recent one, which is exactly last-in-first-out. Keep a stack of valid scores:

  • integer: push it
  • "+": push `stack[-1] + ...

Submit Your Answer
Markdown supported