Evaluate Increment and Decrement Commands

Quick Overview

Evaluate Increment and Decrement Commands. Start integer X at zero. Each command in operations is one of ++X, X++, --X, or X--. The first two add one and the latter two subtract one. Return X after the entire command list.

Evaluate Increment and Decrement Commands

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem The integer variable `X` starts at 0. Each entry of `operations` is `++X`, `X++`, `--X` or `X--`. The first two increase X by one; the other two decrease it by one. Return its value after all commands have been applied. ```cpp class Solution { public: int finalValueAfterOperations(vector<string>& operations); }; ``` ### Examples **Example 1:** ```text operations = ["X--","--X","++X"] Output: -1 Explanation: The values are -1, -2, then -1. ``` **Example 2:** ```text operations = ["++X","X--"] Output: 0 Explanation: One increment and one decrement cancel. ``` ### Constraints - `1 <= operations.length <= 100`. - Every command is exactly `++X`, `X++`, `--X`, or `X--`. ### Clarifying Questions Prefix and postfix spellings have the same effect here because no expression uses the old or new value. ```hint Inspect the structure Which fixed character position distinguishes increments from decrements in all four spellings? ``` ### Reasoning checkpoints - Interpret all four permitted spellings with the stated initial value and effect. - Handle negative intermediate values and cancellation across the complete command list. ### Optional extensions Would reordering this particular command list change the final result?

Overview: Evaluate Increment and Decrement Commands. Start integer X at zero. Each command in operations is one of ++X, X++, --X, or X--. The first two add one and the latter two subtract one. Return X after the entire command list.

Google logo
Google
May 26, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

The integer variable X starts at 0. Each entry of operations is ++X, X++, --X or X--. The first two increase X by one; the other two decrease it by one. Return its value after all commands have been applied.

class Solution {
public:
    int finalValueAfterOperations(vector<string>& operations);
};

Examples

Example 1:

operations = ["X--","--X","++X"]
Output: -1
Explanation: The values are -1, -2, then -1.

Example 2:

operations = ["++X","X--"]
Output: 0
Explanation: One increment and one decrement cancel.

Constraints

  • 1 <= operations.length <= 100 .
  • Every command is exactly ++X , X++ , --X , or X-- .

Clarifying Questions Guidance

Prefix and postfix spellings have the same effect here because no expression uses the old or new value.

Reasoning checkpoints

  • Interpret all four permitted spellings with the stated initial value and effect.
  • Handle negative intermediate values and cancellation across the complete command list.

Optional extensions

Would reordering this particular command list change the final result?