Validate Stack Sequences

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.
Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initially empty stack, or false otherwise.
LeetCode Problem - 946
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Stack<Integer> stack = new Stack<>(); // Stack to simulate the push and pop operations
int poppedIdx = 0; // Index to track the current element in the 'popped' array
// Iterate over each element in the 'pushed' array
for (int num : pushed) {
stack.push(num); // Simulate pushing the current element onto the stack
// While the stack is not empty and the top element of the stack matches the current element in 'popped'
while (!stack.isEmpty() && stack.peek() == popped[poppedIdx]) {
stack.pop(); // Pop the element from the stack
poppedIdx++; // Move to the next element in the 'popped' array
}
}
// If the stack is empty, it means all elements were popped in the correct order
return stack.isEmpty();
}
}




