Design Browser History (Forward/Back)
Initialize BrowserHistory with homepage
Create the back stack with the homepage 'leetcode.com' as the only node. The forward stack is empty initially.
self.back_stack = [homepage]
self.forward_stack = []Visit 'google.com'
Add 'google.com' to the back stack and clear the forward stack (which is already empty).
self.back_stack.append(url)
self.forward_stack.clear()Visit 'facebook.com'
Add 'facebook.com' to the back stack and clear the forward stack again.
self.back_stack.append(url)
self.forward_stack.clear()Visit 'youtube.com'
Add 'youtube.com' to the back stack and clear the forward stack.
self.back_stack.append(url)
self.forward_stack.clear()Back 1 step
Move one page from back stack to forward stack, going back from 'youtube.com' to 'facebook.com'.
self.forward_stack.append(self.back_stack.pop())
steps -= 1Back 1 step again
Move one more page from back stack to forward stack, going back from 'facebook.com' to 'google.com'.
self.forward_stack.append(self.back_stack.pop())
steps -= 1Forward 1 step
Move one page from forward stack back to back stack, going forward from 'google.com' to 'facebook.com'.
self.back_stack.append(self.forward_stack.pop())
steps -= 1Visit 'linkedin.com' after going back and forward
Visit a new page 'linkedin.com', add it to back stack and clear forward stack, removing 'youtube.com' from forward history.
self.back_stack.append(url)
self.forward_stack.clear()Forward 2 steps (no effect)
Attempt to move forward 2 steps, but forward stack is empty, so stay on 'linkedin.com'.
while steps > 0 and self.forward_stack:
self.back_stack.append(self.forward_stack.pop())
steps -= 1Back 2 steps
Move two pages from back stack to forward stack, going back from 'linkedin.com' to 'google.com'.
while steps > 0 and len(self.back_stack) > 1:
self.forward_stack.append(self.back_stack.pop())
steps -= 1Back 7 steps (limited by history)
Attempt to move back 7 steps, but only 1 page left before homepage, so move back to 'leetcode.com'.
while steps > 0 and len(self.back_stack) > 1:
self.forward_stack.append(self.back_stack.pop())
steps -= 1class BrowserHistory:
def __init__(self, homepage: str):
self.back_stack = [homepage] # STEP 1
self.forward_stack = [] # STEP 1
def visit(self, url: str) -> None:
self.back_stack.append(url) # STEP 2,3,4,8
self.forward_stack.clear() # STEP 2,3,4,8
def back(self, steps: int) -> str:
while steps > 0 and len(self.back_stack) > 1:
self.forward_stack.append(self.back_stack.pop()) # STEP 5,6,10,11
steps -= 1
return self.back_stack[-1] # STEP 5,6,7,9,10,11
def forward(self, steps: int) -> str:
while steps > 0 and self.forward_stack:
self.back_stack.append(self.forward_stack.pop()) # STEP 7,9
steps -= 1
return self.back_stack[-1] # STEP 7,9
if __name__ == '__main__':
browserHistory = BrowserHistory("leetcode.com") # STEP 1
browserHistory.visit("google.com") # STEP 2
browserHistory.visit("facebook.com") # STEP 3
browserHistory.visit("youtube.com") # STEP 4
print(browserHistory.back(1)) # STEP 5
print(browserHistory.back(1)) # STEP 6
print(browserHistory.forward(1)) # STEP 7
browserHistory.visit("linkedin.com") # STEP 8
print(browserHistory.forward(2)) # STEP 9
print(browserHistory.back(2)) # STEP 10
print(browserHistory.back(7)) # STEP 11Key Takeaways
This is hard to see from code alone because the stacks' interaction is implicit; visualization makes it explicit.
Understanding this clearing behavior is crucial to grasp why forward navigation sometimes does nothing.
This boundary condition is subtle in code but clear when watching the stacks shrink visually.
