Veda Bites are swipeable micro-lessons — each one teaches exactly one
idea. The bites below come from this kit — swipe through them in the app.
💡 Key Idea
Stack: LIFO Principle
Last in, first out—the core rule.
A stack is a linear data structure that follows the LIFO (Last-In, First-Out) principle. The most recently added element is the first one to be removed. Think of a stack of plates: you add and remove from the top only.
↳ Stacks enforce LIFO: the last element added is the first removed.
📖 Definition
Core Stack Operations
Push, pop, peek—the big three.
↳ Push adds, pop removes, peek inspects—all at the top.
⭐ Important Fact
Stack Time Complexities
All operations are O(1).
In a well-implemented stack (using an array or linked list), push, pop, and peek all run in O(1) time. This makes stacks extremely efficient for their intended use.
↳ Stack operations are constant-time, O(1).
🌍 Real World
Real-World Stack Examples
Stacks are everywhere in computing.
↳ Stacks power undo, browser navigation, and function calls.
🛠️ Application
Function Call Stack
How recursion is managed.
When a function is called, its context (local variables, return address) is pushed onto the call stack. When the function returns, that context is popped. This is why recursion uses stack memory and can cause stack overflow if too deep.
↳ The call stack manages function calls via push and pop.
✏️ Example
Stack in Action: Browser Back Button
Click back? That's a pop.
As you visit pages, each URL is pushed onto a stack. Clicking 'Back' pops the current page and reveals the previous one. The order is always LIFO—your most recent page is the first to go.
↳ Browser navigation uses a stack to track page history.
💻 Code Example
Implementing a Stack in Python
Simple list-based stack.
↳ Python lists can act as stacks using append() and pop().
➗ Formula
Stack Size Formula
Know your stack's capacity.
↳ Stack size = top index + 1.
⚙️ Process
Checking Balanced Parentheses
A classic stack application.
↳ Stacks elegantly solve balanced parentheses checking.
⚠️ Common Mistake
Popping from an Empty Stack
Underflow is a real bug.
↳ Always check for empty before pop/peek.
✅ Best Practice
Choosing Stack Implementation
Array vs. linked list.
↳ Use array-based for simplicity and cache, linked-list for dynamic size.
🎯 Exam Tip
Stack in Expression Evaluation
Postfix? Use a stack.
In exams, you may be asked to evaluate a postfix expression. Use a stack: push operands, and when you see an operator, pop two operands, apply the operator, and push the result.
↳ Postfix evaluation is a classic stack application.
🧠 Memory Trick
Remember LIFO with Plates
Plates on a spring holder.
Visualize a spring-loaded stack of plates in a cafeteria. You always take the top plate (last placed), and new plates are added on top. That's LIFO.
↳ LIFO = like a stack of plates.
❓ Did You Know
Stack Overflow Origin
The name comes from stacks.
The popular programming Q&A site Stack Overflow gets its name from the common error that occurs when a program's call stack exceeds its memory limit—a fitting name for a site that helps fix such errors.
↳ Stack Overflow is named after the call stack error.