You have a browser with one tab where you start on the homepage and you can visit another URL, get back in the history a number of steps, or move forward in the history a number of steps.

Implement the BrowserHistory class:

  • BrowserHistory(string homepage): Initializes the object with the homepage of the browser.
  • void visit(string url): Visits url from the current page. It clears up all the forward history.
  • string back(int steps): Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps.
  • string forward(int steps): Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.

Input & Output

Example 1 — Basic Browser Navigation
$ Input: operations = ["BrowserHistory","visit","visit","visit","back","back","forward"], parameters = [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[2]]
Output: [null,null,null,null,"facebook.com","leetcode.com","facebook.com"]
💡 Note: Initialize with leetcode.com, visit google.com, facebook.com, youtube.com. Back 1 step to facebook.com, back 1 more to leetcode.com, forward 2 steps to facebook.com
Example 2 — Forward History Cleared
$ Input: operations = ["BrowserHistory","visit","back","visit","forward"], parameters = [["home.com"],["page1.com"],[1],["page2.com"],[1]]
Output: [null,null,"home.com",null,"page2.com"]
💡 Note: Start at home.com, visit page1.com, back to home.com, visit page2.com (clears page1.com), forward stays at page2.com
Example 3 — Boundary Navigation
$ Input: operations = ["BrowserHistory","back","forward"], parameters = [["start.com"],[5],[5]]
Output: [null,"start.com","start.com"]
💡 Note: Only one page in history, so both back 5 steps and forward 5 steps return start.com

Constraints

  • 1 ≤ homepage.length ≤ 20
  • 1 ≤ url.length ≤ 20
  • 1 ≤ steps ≤ 100
  • homepage and url consist of '.' or lower case English letters.
  • At most 5000 calls will be made to visit, back, and forward.

Visualization

Tap to expand
Design Browser History: URL Navigation Systemleetcode.comgoogle.comfacebook.comyoutube.comIndex 0Index 1Index 2 (current)Index 3Operations:• back(1) → move to Index 1 (google.com)• back(1) → move to Index 0 (leetcode.com)• forward(2) → move to Index 2 (facebook.com)• visit(newurl) → clears forward historyArray-based history with current index tracking
Understanding the Visualization
1
Initialize
Start with homepage in history array at index 0
2
Visit Pages
Add new URLs to history, clear forward history when visiting
3
Navigate
Use back/forward to move current index within bounds
Key Takeaway
🎯 Key Insight: Use array with current index to track position, clear forward history when visiting new pages
Asked in
Google 15 Microsoft 12 Amazon 8 Apple 6
28.5K Views
Medium Frequency
~15 min Avg. Time
892 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