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 INPUT leetcode.com (homepage) Operations: 1. BrowserHistory("leetcode") 2. visit("google.com") 3. visit("facebook.com") 4. visit("youtube.com") 5. back(1) 6. back(1) 7. forward(2) History Array Structure: leet google fb yt ALGORITHM STEPS 1 Initialize Array with homepage, curr=0 2 Visit(url) curr++, add url, clear forward 3 Back(steps) curr = max(0, curr-steps) 4 Forward(steps) curr = min(size-1, curr+steps) Execution Trace: [leet] curr=0 [leet,goog,fb,yt] curr=3 back(1): curr=2 --> "facebook" back(1): curr=1 --> "google" wait no fwd(2): curr=3 --> "facebook" FINAL RESULT Output Array: [null, null, null, null, "facebook", "leetcode", "facebook"] Final History State: leetcode idx 0 google idx 1 facebook curr=2 current OK - All Operations Complete back(1): facebook.com back(1): leetcode.com forward(2): facebook.com Complexity: Time: O(1) per operation Space: O(n) for history Key Insight: Use a dynamic array with a current pointer to track position. On visit(), increment pointer and truncate forward history. back() moves pointer left (bounded by 0), forward() moves right (bounded by array size). This achieves O(1) time for all operations with efficient capacity management. TutorialsPoint - Design Browser History | Dynamic Array with Capacity Management
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