Design Browser History - Complete Solution Guide
Design Browser History is LeetCode problem 1472, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You have a browser of one tab where you start on the homepage and you can visit another url , get back in the history number of steps or move forward in the history 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
Detailed Explanation
The problem asks us to simulate a simplified browser history. We need to implement a `BrowserHistory` class that allows us to visit URLs, go back a certain number of steps in history, and go forward a certain number of steps. The browser starts at a given `homepage`. Visiting a new URL clears any forward history. The `back` and `forward` methods should move as many steps as possible within the available history, returning the current URL after the move. The constraints limit the number of calls to each method and the length of the URLs.
Solution Approach
The provided solution uses an array (or List in Java/C++) to store the browsing history. `current_index` points to the currently visited URL, and `last_index` indicates the last valid index in the history array. The `visit` method appends the new URL to the history and updates both `current_index` and `last_index`. If the new `url` is visited after navigating `back`, then all URLs from `current_index + 1` onward are implicitly removed (overwritten), and `last_index` is updated accordingly. The `back` and `forward` methods simply adjust `current_index` within the bounds of the history and return the URL at the updated index.
Step-by-Step Algorithm
- Step 1: Initialize the `BrowserHistory` object with the `homepage` by creating a list and adding the `homepage` to it. Set `current_index` and `last_index` to 0.
- Step 2: `visit(url)`: Increment `current_index`. If it's beyond the current `last_index` (or list size), append the `url` to the history list. Otherwise, overwrite the existing URL at the `current_index`. Update `last_index` to be equal to `current_index`.
- Step 3: `back(steps)`: Move `current_index` back by `steps`, but ensure it doesn't go below 0. Set `current_index = max(0, current_index - steps)`. Return the URL at the new `current_index`.
- Step 4: `forward(steps)`: Move `current_index` forward by `steps`, but ensure it doesn't go beyond `last_index`. Set `current_index = min(last_index, current_index + steps)`. Return the URL at the new `current_index`.
Key Insights
- Insight 1: A simple array or list is sufficient to store the history of visited URLs.
- Insight 2: We need to maintain a `current_index` to track the currently visited page and a `last_index` to track the end of the valid history.
- Insight 3: The `back` and `forward` methods need to handle edge cases where the requested number of steps exceeds the available history.
Complexity Analysis
Time Complexity: O(1)
Space Complexity: O(n)
Topics
This problem involves: Array, Linked List, Stack, Design, Doubly-Linked List, Data Stream.
Companies
Asked at: Cisco, DoorDash, Roblox, Snap, Splunk.