Imagine you're building the core navigation system for a web browser! ๐ŸŒ You need to implement a BrowserHistory class that manages the browsing history with familiar operations like visiting new pages, going back, and moving forward.

Your browser starts on a homepage and supports these key operations:

  • Visit a URL: Navigate to a new page (this clears any forward history, just like real browsers!)
  • Go Back: Move back through your browsing history by a specified number of steps
  • Go Forward: Move forward through your browsing history by a specified number of steps

The challenge is to efficiently track the current position and handle the dynamic nature of web browsing where visiting a new page from the middle of history erases all forward pages.

Example: Start at "homepage" โ†’ visit "google.com" โ†’ visit "youtube.com" โ†’ go back 1 step (now at "google.com") โ†’ visit "facebook.com" (this erases "youtube.com" from forward history)

Input & Output

example_1.py โ€” Basic Navigation
$ Input: BrowserHistory browserHistory = new BrowserHistory("leetcode.com"); browserHistory.visit("google.com"); browserHistory.visit("facebook.com"); browserHistory.visit("youtube.com"); browserHistory.back(1); // return "facebook.com" browserHistory.back(1); // return "google.com" browserHistory.forward(1); // return "facebook.com" browserHistory.visit("linkedin.com"); browserHistory.forward(2); // return "linkedin.com"
โ€บ Output: [null, null, null, null, "facebook.com", "google.com", "facebook.com", null, "linkedin.com"]
๐Ÿ’ก Note: Start at leetcode.com, visit 3 sites building history [leetcode.com, google.com, facebook.com, youtube.com]. Go back twice to google.com, forward once to facebook.com. Visit linkedin.com (truncates youtube.com from history). Try to go forward 2 steps but can only stay at linkedin.com.
example_2.py โ€” Forward History Clearing
$ Input: BrowserHistory browserHistory = new BrowserHistory("homepage"); browserHistory.visit("page1"); browserHistory.visit("page2"); browserHistory.back(1); // return "page1" browserHistory.visit("page3"); // clears page2 from forward history browserHistory.forward(1); // return "page3" (can't go to page2)
โ€บ Output: [null, null, null, "page1", null, "page3"]
๐Ÿ’ก Note: After going back to page1 and visiting page3, the forward history (page2) is completely erased. This is exactly how real browsers behave - visiting a new page from the middle of history creates a new branch.
example_3.py โ€” Boundary Conditions
$ Input: BrowserHistory browserHistory = new BrowserHistory("start"); browserHistory.back(5); // return "start" (can't go back further) browserHistory.forward(3); // return "start" (no forward history) browserHistory.visit("end"); browserHistory.back(10); // return "start" (limited by available history)
โ€บ Output: [null, "start", "start", null, "start"]
๐Ÿ’ก Note: Demonstrates boundary conditions: trying to go back/forward more steps than available should stop at the furthest possible position. With only 2 pages total, going back 10 steps stops at the first page.

Visualization

Tap to expand
Browser History Implementationhomepageindex: 0[current]google.comindex: 1facebook.comindex: 2youtube.comindex: 3forwardhistoryCurrent Positionvisit() โ†’ move forwardback() โ†’ move backwardKey Operations:โ€ข visit(url): currentIndex++, truncate array after current, append new urlโ€ข back(steps): currentIndex = max(0, currentIndex - steps)โ€ข forward(steps): currentIndex = min(length-1, currentIndex + steps)
Understanding the Visualization
1
Initialize
Start with homepage in history array, current position at index 0
2
Visit Pages
Each visit adds to history and moves current position forward
3
Navigate Back
Moving back decreases current position (bounded by 0)
4
Navigate Forward
Moving forward increases current position (bounded by history length)
5
Visit from Middle
Visiting a new page truncates all forward history and adds new page
Key Takeaway
๐ŸŽฏ Key Insight: Browser history is simply an array with a current position pointer. The magic happens when visiting a new page - it truncates all forward history, creating a linear browsing experience just like real browsers!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Back and forward are O(1) pointer movements. Visit is O(1) amortized for array operations.

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Store at most n URLs in the history array where n is the number of pages visited

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค homepage.length โ‰ค 20
  • 1 โ‰ค url.length โ‰ค 20
  • 1 โ‰ค steps โ‰ค 100
  • homepage and url consist of '.' or lowercase English letters
  • At most 5000 calls will be made to visit, back, and forward
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
89.2K Views
High Frequency
~18 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen