Final Value of Variable After Performing Operations

Easy
1113.

Final Value of Variable After Performing Operations

Final Value of Variable After Performing Operations is an easy coding problem on arrays, strings and simulation, asked in Amazon and Google interviews.

A tiny language has one variable X, which starts at 0, and four statements:

  • "++X" and "X++" add 1 to X;
  • "--X" and "X--" subtract 1 from X.

Given the statements in operations, run them in order and return the final value of X.

Example 1

Input
operations = ["X++","X++","--X"]
Output
1
Explanation
0 → 1 → 2 → 1.

Example 2

Input
operations = ["--X","X--","X--","++X"]
Output
-2

Example 3

Input
operations = ["++X"]
Output
1

Constraints

  • 1 <= operations.length <= 100
  • operations[i] is one of "++X", "X++", "--X" or "X--"
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string[]} operations
 * @return {number}
 */
var finalValueAfterOperations = function(operations) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Final Value of Variable After Performing Operations Solution

Final Value of Variable After Performing Operations solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript.

Final Value of Variable After Performing Operations is an easy coding problem on arrays, strings and simulation, asked in Amazon and Google interviews.

  • Difficulty: Easy
  • Topics: Arrays, Strings, Simulation
  • Asked at: Amazon, Google
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

A tiny language has one variable X, which starts at 0, and four statements:

  • "++X" and "X++" add 1 to X;
  • "--X" and "X--" subtract 1 from X.

Given the statements in operations, run them in order and return the final value of X.

Example 1

Input: operations = ["X++","X++","--X"]
Output: 1
Explanation: 0 → 1 → 2 → 1.

Example 2

Input: operations = ["--X","X--","X--","++X"]
Output: -2

Example 3

Input: operations = ["++X"]
Output: 1

Constraints

  • 1 <= operations.length <= 100
  • operations[i] is one of "++X", "X++", "--X" or "X--"

Hints for Final Value of Variable After Performing Operations

  1. Hint 1

    Prefix and postfix forms do the same thing here — nothing reads X mid-statement.

  2. Hint 2

    Every statement is three characters, and its middle character is always the operator's sign.

  3. Hint 3

    Add 1 when the middle character is +, subtract 1 otherwise.

How to solve Final Value of Variable After Performing Operations

Each statement changes X by exactly ±1, and the sign can be read from its middle character, which is + or - in all four forms.

Approach

  1. Start with x = 0.
  2. For each statement, add 1 if op[1] == '+', else subtract 1.
  3. Return x.

Why it works

All four statements are three characters long and the character at index 1 is a sign in every one of them (+/- in ++X, X++, --X, X--). The order of statements does not matter for a sum of ±1 steps, so a single pass gives the final value.

Complexity

  • Time — O(n)
  • Space — O(1)

Pitfalls

  • Checking only the first character fails for the postfix forms, whose first character is X.
  • Pre- and post-increment differ only in what an expression would read, not in the final value.
  • The result can be negative.

Reference solution

Python

from typing import List

def finalValueAfterOperations(operations: List[str]) -> int:
    return sum(1 if op[1] == '+' else -1 for op in operations)

Java

public static int finalValueAfterOperations(String[] operations) {
    int x = 0;
    for (String op : operations) {
        x += op.charAt(1) == '+' ? 1 : -1;
    }
    return x;
}

C++

int finalValueAfterOperations(vector<string>& operations) {
    int x = 0;
    for (const string& op : operations) {
        x += op[1] == '+' ? 1 : -1;
    }
    return x;
}

JavaScript

var finalValueAfterOperations = function(operations) {
    var x = 0;
    for (var i = 0; i < operations.length; i++) {
        x += operations[i].charAt(1) === '+' ? 1 : -1;
    }
    return x;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays · Strings