LESSON 66 / 75·2 min readIntermediate

Stack and queue in Python

Should knowConcept lesson
01 Understand02 Recall03 Prepare

Stack

Python
stack = []
stack.append(1)
stack.append(2)
stack.pop()

Queue

Python
from collections import deque
queue = deque()
queue.append(1)
queue.append(2)
queue.popleft()

Problems to practice

  • Valid Parentheses
  • Min Stack
  • Daily Temperatures
  • Sliding Window Maximum
  • Largest Rectangle in Histogram
  • Implement Queue Using Stack

Example: valid parentheses

Python
def is_valid(s):
    stack = []
    pairs = {')': '(', ']': '[', '}': '{'}

    for ch in s:
        if ch in '([{':
            stack.append(ch)
        else:
            if not stack or stack.pop() != pairs[ch]:
                return False

    return not stack

PAUSE & RECALL

Can you explain it without the notes?

Stack and queue in Python

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

Stack

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 Python

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