Skip to main content

Command Palette

Search for a command to run...

Final Value of Variable After Performing Operations

Published
1 min read
Final Value of Variable After Performing Operations
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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.

LeetCode Problem - 2011

class Solution {
    // Method to calculate the final value after a series of operations
    public int finalValueAfterOperations(String[] operations) {
        // Initialize a count variable to keep track of the value
        int count = 0;

        // Iterate through each operation in the array
        for (String operation : operations) {
            // If the operation is "++X" or "X++", increment the count
            if ((operation.equals("++X")) || (operation.equals("X++"))) {
                count++;
            } 
            // If the operation is "--X" or "X--", decrement the count
            else {
                count--;
            }
        }

        // Return the final value after all operations
        return count;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.