Bird
Raised Fist0
Interview Preplinked-list-advancedmediumAmazonGoogleFacebook

Design Browser History (Forward/Back)

Choose your preparation mode3 modes available
Steps
setup

Initialize BrowserHistory with homepage

Create the back stack with the homepage 'leetcode.com' as the only node. The forward stack is empty initially.

💡 Initialization sets the starting point for browsing history, ensuring there is always at least one page in the back stack.
Line:self.back_stack = [homepage] self.forward_stack = []
💡 The homepage is the base of the back stack and navigation cannot go back beyond it.
📊
Design Browser History (Forward/Back) - Watch the Algorithm Execute, Step by Step
Watching each operation step-by-step reveals how the back and forward stacks interact, clarifying the logic behind clearing forward history on visits and limiting back navigation.
Step 1/11
·Active fillAnswer cell
setup
leetcode.com
insert
leetcode.com
google.com
insert
leetcode.com
google.com
facebook.com
insert
leetcode.com
google.com
facebook.com
youtube.com
shrink
leetcode.com
google.com
facebook.com
youtube.com
Result: facebook.com
shrink
leetcode.com
google.com
facebook.com
youtube.com
Result: google.com
expand
leetcode.com
google.com
facebook.com
youtube.com
Result: facebook.com
insert
leetcode.com
google.com
facebook.com
youtube.com
linkedin.com
expand
leetcode.com
google.com
facebook.com
linkedin.com
youtube.com
Result: linkedin.com
shrink
leetcode.com
google.com
facebook.com
linkedin.com
youtube.com
Result: google.com
shrink
leetcode.com
google.com
facebook.com
linkedin.com
youtube.com
Result: leetcode.com

Key Takeaways

The back and forward stacks represent the browsing history and future navigation paths respectively.

This is hard to see from code alone because the stacks' interaction is implicit; visualization makes it explicit.

Visiting a new page clears the forward stack, preventing forward navigation beyond the new page.

Understanding this clearing behavior is crucial to grasp why forward navigation sometimes does nothing.

Back navigation stops at the homepage, ensuring there is always at least one page in the back stack.

This boundary condition is subtle in code but clear when watching the stacks shrink visually.