1. Concept library
  2. Data Structures & Algorithms
  3. Module 5: Stacks and queues

Concept

Stack applications

A stack is last in, first out. It fits anything that nests or must be undone in reverse: matching brackets, evaluating expressions, backtracking, and the call stack itself.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain last in, first out and name three problems it fits
  2. Write a bracket matcher with a stack
  3. Spot the bug of returning true before checking the stack is empty

In practice

A few lines that show Stack applications doing its job.

Python
def balanced(text):
    pairs = {")": "(", "]": "[", "}": "{"}
    stack = []
    for ch in text:
        if ch in "([{":
            stack.append(ch)
        elif ch in pairs:
            if not stack or stack.pop() != pairs[ch]:
                return False
    return not stack

balanced("{[()]}")   # True

The common mistake

Watch for

Returning True as soon as the loop finishes. Unclosed openers are still on the stack, so "((" would pass; the stack must be empty at the end.

What to know first

Your roadmap puts these before Stack applications, unless you already know them.

How it stays learned

Once a session shows you understand Stack applications, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on Stack applications.

Next in the moduleQueues and deques →