Have you ever accidentally closed a browser tab and immediately thought
"Oh no! I needed that tab!" š
Luckily, Chrome gives us Ctrl + Shift + T ā or the "Reopen Closed Tab" option ā to bring it back.
But have you ever wondered what happens behind the scenes?
This simple browser feature is actually a great example of a fundamental Data Structure: the Stack.
š§ The Interview Question
Imagine you're in a technical interview and the interviewer asks:
"You accidentally closed a browser tab. Chrome restores it instantly using 'Reopen Closed Tab'. Which data structure would you use to design this feature?"
The answer is:
š Stack
More specifically, a Stack follows the LIFO principle:
LIFO = Last In, First Out
And that's exactly how recently closed tabs can be modeled.
š What Is a Stack?
A stack is a linear data structure where elements are added and removed from the same end.
Think about a stack of plates š½ļø.
If you place plates one on top of another:
āāāāāāāāāāāāā
ā Plate 3 ā ā Last added
āāāāāāāāāāāāā¤
ā Plate 2 ā
āāāāāāāāāāāāā¤
ā Plate 1 ā ā First added
āāāāāāāāāāāāā
plaintext
Which plate will you remove first?
Plate 3, because it was the last one added.
That's LIFO ā Last In, First Out.
š Applying the Stack to Browser Tabs
Let's say you have these tabs open:
Google ā YouTube ā GitHub ā Gmail
plaintext
Now you start closing them.
First:
Close Gmail
plaintext
The closed-tab stack becomes:
Top
ā
[Gmail]
plaintext
Then:
Close GitHub
plaintext
Now:
Top
ā
[GitHub]
[Gmail]
plaintext
Then:
Close YouTube
plaintext
The stack becomes:
Top
ā
[YouTube]
[GitHub]
[Gmail]
plaintext
Now you press:
Ctrl + Shift + T
What should Chrome restore?
YouTube.
Why?
Because YouTube was the last tab you closed.
That's exactly what a stack does.
š Push and Pop
A stack mainly uses two operations:
1. Push
push() adds an element to the top of the stack.
When a user closes a tab:
Closed Tab ā push()
plaintext
For example:
push(Gmail)
push(GitHub)
push(YouTube)
plaintext
The stack becomes:
Top
ā
YouTube
GitHub
Gmail
plaintext
2. Pop
pop() removes the top element.
When the user selects Reopen Closed Tab:
pop()
plaintext
The result is:
YouTube
plaintext
The stack then becomes:
Top
ā
GitHub
Gmail
plaintext
If the user presses Ctrl + Shift + T again:
pop() ā GitHub
plaintext
And again:
pop() ā Gmail
javascript
So the tabs are restored in the reverse order in which they were closed.
š» How Would We Design It?
We can model this behavior using a simple JavaScript class:
class ClosedTabs {
constructor() {
this.stack = [];
}
closeTab(tab) {
this.stack.push(tab);
}
reopenTab() {
if (this.stack.length === 0) {
return null;
}
return this.stack.pop();
}
}
Let's use it:
const closedTabs = new ClosedTabs();
closedTabs.closeTab("Gmail");
closedTabs.closeTab("GitHub");
closedTabs.closeTab("YouTube");
console.log(closedTabs.reopenTab());
// YouTube
console.log(closedTabs.reopenTab());
// GitHub
console.log(closedTabs.reopenTab());
// Gmail
The output is:
YouTube
GitHub
Gmail
Exactly what we would expect from a LIFO structure.
šļø But We Wouldn't Store Only the Tab Name
In a real browser, restoring a tab involves much more information than its title.
A closed-tab object could conceptually contain:
const tab = {
url: "https://github.com",
title: "GitHub",
scrollPosition: 450,
history: [
"https://google.com",
"https://github.com"
],
timestamp: Date.now()
};
So when the tab is restored, the browser can potentially restore relevant session information as well.
Important: This is a simplified data-structure model. Real browser session restoration is more sophisticated than simply maintaining one JavaScript stack.
ā±ļø Time Complexity
One of the biggest advantages of using a stack is that its basic operations are very efficient.
| Operation | Complexity |
|---|---|
| Push | O(1) |
| Pop | O(1) |
| Peek | O(1) |
So:
Close Tab ā O(1)
Reopen Tab ā O(1)
That's extremely efficient.
š¤ Why Not a Queue?
A common interview follow-up could be:
"Why not use a Queue?"
A queue follows:
FIFO ā First In, First Out
Imagine we close:
Gmail
GitHub
YouTube
A queue would return:
Gmail ā GitHub ā YouTube
But for a recently closed tab feature, we generally want:
YouTube ā GitHub ā Gmail
That's LIFO, which makes a stack a natural model.
š§© Stack vs Queue
STACK
LIFO
Push ā [A] [B] [C] ā Pop
ā
First out
Whereas:
QUEUE
FIFO
Enqueue ā [A] [B] [C] ā Dequeue
ā ā
First in First out
This is why the distinction between LIFO and FIFO is so important in Data Structures interviews.
š Real-World Applications of Stacks
The Chrome example isn't the only place where stacks are useful.
Stacks are commonly used in:
- ā©ļø Undo/Redo operations
- š Browser navigation
- š§® Expression evaluation
- š Function call management
- š§© Backtracking algorithms
- š Syntax parsing
- š Depth-First Search (DFS)
- š Parentheses matching
For example, when you press Ctrl + Z, previous actions can be modeled using stack-based behavior.
šÆ The Interview Takeaway
If an interviewer asks:
"A user closes several browser tabs and wants to restore the most recently closed tab first. Which data structure would you use?"
Think:
Most Recent ā First Restored
ā
LIFO
ā
STACK
The basic design is:
Close Tab
ā
push()
ā
Stack
ā
pop()
ā
Reopen Tab
š” Final Thought
Sometimes the best way to understand Data Structures isn't by memorizing definitions.
It's by looking at the software we use every day.
That tiny "Reopen Closed Tab" button is a perfect example of how a fundamental concept like a Stack can help us reason about real-world software behavior.
So next time you accidentally close a tab and press:
Ctrl + Shift + T
remember:
Last closed. First restored. LIFO. Stack. š
And that's Data Structures hiding in plain sight.
Written by Kashaf Abdullah
Software Engineer | MERN Stack | Web Development
Found this helpful? Leave a ā¤ļø and bookmark it for later!
Let's discuss in the comments: Have you ever thought about the data structures behind everyday browser features? What other examples can you think of?

Top comments (0)