Watch 10 video solutions for Final Value of Variable After Performing Operations, a easy level problem involving Array, String, Simulation. This walkthrough by CodeClips with Abhishek Ranjan has 2,313 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
There is a programming language with only four operations and one variable X:
++X and X++ increments the value of the variable X by 1.--X and X-- decrements the value of the variable X by 1.Initially, the value of X is 0.
Given an array of strings operations containing a list of operations, return the final value of X after performing all the operations.
Example 1:
Input: operations = ["--X","X++","X++"] Output: 1 Explanation: The operations are performed as follows: Initially, X = 0. --X: X is decremented by 1, X = 0 - 1 = -1. X++: X is incremented by 1, X = -1 + 1 = 0. X++: X is incremented by 1, X = 0 + 1 = 1.
Example 2:
Input: operations = ["++X","++X","X++"] Output: 3 Explanation: The operations are performed as follows: Initially, X = 0. ++X: X is incremented by 1, X = 0 + 1 = 1. ++X: X is incremented by 1, X = 1 + 1 = 2. X++: X is incremented by 1, X = 2 + 1 = 3.
Example 3:
Input: operations = ["X++","++X","--X","X--"] Output: 0 Explanation: The operations are performed as follows: Initially, X = 0. X++: X is incremented by 1, X = 0 + 1 = 1. ++X: X is incremented by 1, X = 1 + 1 = 2. --X: X is decremented by 1, X = 2 - 1 = 1. X--: X is decremented by 1, X = 1 - 1 = 0.
Constraints:
1 <= operations.length <= 100operations[i] will be either "++X", "X++", "--X", or "X--".Problem Overview: You start with a variable X = 0. An array of strings contains operations such as ++X, X++, --X, and X--. Each increment operation increases X by 1 and each decrement operation decreases it by 1. The task is to process the list of operations and return the final value after all updates.
Approach 1: Iterative Count Approach (Time: O(n), Space: O(1))
Traverse the operations array once and update a counter that represents X. For every string, check whether it represents an increment or decrement. A simple check like if '+' in operation means the value increases; otherwise it decreases. This is a direct simulation of the operations and mirrors exactly what the problem statement describes. Since you only scan the array once and keep a single integer variable, the time complexity is O(n) and the space complexity is O(1). This approach works in any language and is the most straightforward solution.
Approach 2: Using String Count Method (Time: O(n), Space: O(1))
Instead of updating the variable step-by-step, count how many increment and decrement operations exist. Each operation string always contains either '+' or '-'. By counting how many strings contain '+' and subtracting the number containing '-', you can compute the final result directly. Languages like Python and JavaScript allow concise implementations using string membership checks or array filtering. The algorithm still scans the list once, so the time complexity remains O(n) with O(1) additional space.
The problem mainly tests basic iteration over an array and simple string inspection. Conceptually, it is a small simulation task: apply each operation exactly as written and track the result.
Recommended for interviews: The iterative simulation approach is what interviewers typically expect. It demonstrates that you read the problem carefully and can translate the operations directly into code with O(n) time and constant space. The string-count variant is equally efficient but mostly a stylistic shortcut available in languages with convenient string operations.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Count (Simulation) | O(n) | O(1) | Best general solution. Directly simulate each operation while scanning the array once. |
| String Count Method | O(n) | O(1) | Useful in Python/JavaScript for concise code using string membership or filtering. |