A stack is one of the simplest data structures in computer science, but it's also incredibly useful.
It follows a simple rule called LIFO: Last In, First Out.
This means the last element you add to a stack is the first one to be removed.
Loading simulation...
Despite its simplicity, stacks are used everywhere:
In this chapter, we'll understand what a stack is and how it works.
We'll explore its basic operations, their time complexities, and different ways to implement a stack.
And finally, we'll look at some common patterns where stacks are useful, especially in coding interviews.
Think about a stack of plates in your kitchen.
You wash a plate and place it on top of the stack. Then you wash another plate and place it on top of the previous one.
Now, when you need a plate, you take the one at the top, which is the plate you added most recently.
You can't take a plate from the middle without removing the ones above it first.
And that's exactly how a stack works. It is a linear data structure where elements are added and removed from only one end, called the top.
A stack supports four common operations.
Push simply adds a new element to the top of the stack.
For example, suppose our stack contains 1, 2, and 3, with 3 at the top.
If we push 4, it gets added above 3 and becomes the new top.
Pop removes the top element and returns it.
So if we pop from our current stack, we get back 4, leaving 1, 2, and 3.
3 is now the top again.
Peek, sometimes called top, lets us see the top element without removing it.
So unlike pop, it doesn't change the stack. Calling peek on the stack above returns 3, and the stack stays 1, 2, 3.
isEmpty checks whether the stack contains any elements.
For example, if we pop 3, then 2, and finally 1, our stack becomes empty.
At this point, isEmpty returns true.
This is particularly useful when we want to avoid popping from an empty stack. Popping from an empty stack is an error, often called a stack underflow.
Here's what makes stacks so efficient.
All four operations take O(1) time, because we only need to access the top of the stack. There's no need to search through elements or shift anything around.
As for space complexity, storing n elements requires O(n) space.
So stacks give us constant-time access to the most recently added element, while using memory proportional to the number of elements stored.
How do we implement a stack ourselves? There are two common approaches: we can use an array or a linked list.
Let's look at both.
The idea is straightforward.
We maintain an array and a variable called top, which keeps track of the index of the top element.
Since all operations happen at the end of the array, they take O(1) time.
top to -1, indicating an empty stack.top and store the new element at that index.top.top without removing it.top is equal to -1.In this code, pushing onto a full stack prints "Stack Overflow", and popping or peeking an empty stack prints a message and returns -1. In production code, you would usually throw an exception instead.
The main limitation here is that our array has a fixed capacity.
Once it's full, we can't push more elements without resizing it.
Fortunately, modern languages provide dynamic arrays, like ArrayList in Java and list in Python, that handle resizing automatically.
In the linked list implementation, the head of the linked list represents the top of our stack.
Whenever we push an element, we create a new node at the beginning of the list.
Here's how it works step by step:
1.1, and make 2 the new head.3 becomes the head.2.1.Since we only work with the head, all these operations take O(1) time.
Here's what the linked list implementation looks like in code.
null.So which implementation should you choose?
An array stores elements together in memory, making it more memory-efficient. But with a fixed array, we need to handle capacity limits and resizing ourselves.
A linked list grows dynamically without resizing, but every node requires an extra pointer, increasing memory overhead.
Both approaches support constant-time stack operations, although resizing a dynamic array can occasionally make a push more expensive.
In practice, you rarely need to implement a stack from scratch.
Most programming languages already provide efficient, well-tested ways to use stacks.
In Java, the recommended approach is to use the Deque interface with ArrayDeque.
We create an ArrayDeque and push three elements onto it. Calling peek returns 3 without removing it. Calling pop removes and returns 3. And isEmpty tells us whether the stack is empty.
Under the hood, ArrayDeque uses a resizable array. Push and pop operate at the same end, giving us O(1) amortized time.
Java also has an older Stack class, but ArrayDeque is generally preferred for stack operations.
All these built-in options are efficient and work well for most use cases.
So unless an interviewer specifically asks you to implement a stack, it's usually better to use the standard library.
Now comes an important question for coding interviews: when should you use a stack?
There are a few common patterns worth recognizing.
And that's the stack data structure.
ArrayDeque in Java, list in Python, or std::stack in C++.To really understand these concepts, I'd recommend implementing a stack yourself using both an array and a linked list.
Working through push, pop, peek, and handling an empty stack will help you build a much stronger understanding of how stacks work behind the scenes.
10 quizzes