Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Validating push pop sequence in JavaScript
Validating push/pop sequences is a common stack problem where we need to verify if a given pop sequence could result from a specific push sequence on an initially empty stack.
Problem Statement
Given two arrays pushed and popped containing unique elements, determine if the pop sequence could be achieved from the push sequence using stack operations.
const pushed = [1, 2, 3, 4, 5]; const popped = [4, 5, 3, 2, 1];
How It Works
The algorithm simulates stack operations by maintaining a stack and two pointers. We push elements until the stack top matches the next element to pop, then pop it. If we can successfully pop all elements in the correct order, the sequence is valid.
Implementation
const validateSequence = (pushed = [], popped = []) => {
let pushedIndex = 0;
let poppedIndex = 0;
const stack = [];
while (pushedIndex < pushed.length) {
if (stack[stack.length - 1] !== popped[poppedIndex]) {
stack.push(pushed[pushedIndex++]);
} else {
stack.pop();
poppedIndex += 1;
}
}
while (stack.length) {
if (stack.pop() !== popped[poppedIndex++]) {
return false;
}
}
return true;
};
// Test cases
const pushed1 = [1, 2, 3, 4, 5];
const popped1 = [4, 5, 3, 2, 1];
console.log("Test 1:", validateSequence(pushed1, popped1));
const pushed2 = [1, 2, 3, 4, 5];
const popped2 = [4, 3, 5, 1, 2];
console.log("Test 2:", validateSequence(pushed2, popped2));
Test 1: true Test 2: false
Step-by-Step Example
For pushed = [1, 2, 3, 4, 5] and popped = [4, 5, 3, 2, 1]:
const traceValidation = (pushed, popped) => {
let pushedIndex = 0, poppedIndex = 0;
const stack = [];
console.log("Initial: stack=[], pushedIndex=0, poppedIndex=0");
while (pushedIndex < pushed.length) {
if (stack[stack.length - 1] !== popped[poppedIndex]) {
stack.push(pushed[pushedIndex++]);
console.log(`Push ${pushed[pushedIndex-1]}: stack=[${stack}]`);
} else {
const popped_val = stack.pop();
poppedIndex++;
console.log(`Pop ${popped_val}: stack=[${stack}]`);
}
}
while (stack.length && stack[stack.length - 1] === popped[poppedIndex]) {
const popped_val = stack.pop();
poppedIndex++;
console.log(`Final pop ${popped_val}: stack=[${stack}]`);
}
return stack.length === 0;
};
const result = traceValidation([1, 2, 3, 4, 5], [4, 5, 3, 2, 1]);
console.log("Valid sequence:", result);
Initial: stack=[], pushedIndex=0, poppedIndex=0 Push 1: stack=[1] Push 2: stack=[1,2] Push 3: stack=[1,2,3] Push 4: stack=[1,2,3,4] Pop 4: stack=[1,2,3] Push 5: stack=[1,2,3,5] Final pop 5: stack=[1,2,3] Final pop 3: stack=[1,2] Final pop 2: stack=[1] Final pop 1: stack=[] Valid sequence: true
Time and Space Complexity
| Complexity | Value | Explanation |
|---|---|---|
| Time | O(n) | Each element is pushed and popped at most once |
| Space | O(n) | Stack can hold up to n elements in worst case |
Conclusion
This algorithm efficiently validates push/pop sequences by simulating stack operations. The key insight is that we can determine validity by maintaining a stack and checking if elements can be popped in the required order.
