Stack and queue in JavaScript
Stack using array
const stack = [];
stack.push(1);
stack.push(2);
stack.pop();Queue using array
const queue = [];
queue.push(1);
queue.push(2);
queue.shift();Problems to practice
- Valid Parentheses
- Min Stack
- Daily Temperatures
- Sliding Window Maximum
- Implement Queue Using Stack
- Implement Stack Using Queue
Example: valid parentheses
function isValid(s) {
const stack = [];
const pairs = { ')': '(', ']': '[', '}': '{' };
for (const ch of s) {
if (['(', '[', '{'].includes(ch)) stack.push(ch);
else {
if (stack.pop() !== pairs[ch]) return false;
}
}
return stack.length === 0;
}Can you explain it without the notes?
Stack and queue in JavaScript
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
Stack using array
Turn this concept into a clear answer.
Practice this conceptAdapted from 16_DSA_JAVASCRIPT.md · “5. Stack and queue in JavaScript”. Examples are study fragments; framework behavior can depend on your version.