LESSON 24 / 75·2 min readIntermediate

Stack and queue in JavaScript

Should knowConcept lesson
01 Understand02 Recall03 Prepare

Stack using array

JavaScript
const stack = [];
stack.push(1);
stack.push(2);
stack.pop();

Queue using array

JavaScript
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

JavaScript
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;
}

PAUSE & RECALL

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

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Recursion and backtracking in JavaScript

Adapted from 16_DSA_JAVASCRIPT.md · “5. Stack and queue in JavaScript”. Examples are study fragments; framework behavior can depend on your version.