Imagine you are building a web browser that allows users to navigate back and forth through their browsing history seamlessly.
Design a browser history system that supports visiting new URLs, moving back by a certain number of steps, and moving forward by a certain number of steps. Implement the BrowserHistory class with the following methods:
- BrowserHistory(string homepage): Initializes the object with the homepage.
- 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. Return the current URL after moving back.
- string forward(int steps): Move steps forward in history. Return the current URL after moving forward.
1 ≤ steps ≤ 1001 ≤ url.length ≤ 20At most 5000 calls will be made to visit, back, and forward.
Edge cases: Back steps more than history length → returns earliest pageForward steps more than forward history → returns latest pageVisit after back truncates forward history
class BrowserHistory:
def __init__(self, homepage: str):
...
def visit(self, url: str) -> None:
...
def back(self, steps: int) -> str:
...
def forward(self, steps: int) -> str:
...public class BrowserHistory {
public BrowserHistory(String homepage) { ... }
public void visit(String url) { ... }
public String back(int steps) { ... }
public String forward(int steps) { ... }
}class BrowserHistory {
public:
BrowserHistory(string homepage) { ... }
void visit(string url) { ... }
string back(int steps) { ... }
string forward(int steps) { ... }
};class BrowserHistory {
constructor(homepage) { ... }
visit(url) { ... }
back(steps) { ... }
forward(steps) { ... }
}
class BrowserHistory:
def __init__(self, homepage: str):
# Write your solution here
pass
def visit(self, url: str) -> None:
pass
def back(self, steps: int) -> str:
pass
def forward(self, steps: int) -> str:
pass
public class BrowserHistory {
public BrowserHistory(String homepage) {
// Write your solution here
}
public void visit(String url) {
}
public String back(int steps) {
return "";
}
public String forward(int steps) {
return "";
}
}
#include <string>
using namespace std;
class BrowserHistory {
public:
BrowserHistory(string homepage) {
// Write your solution here
}
void visit(string url) {
}
string back(int steps) {
return "";
}
string forward(int steps) {
return "";
}
};
class BrowserHistory {
constructor(homepage) {
// Write your solution here
}
visit(url) {
}
back(steps) {
return "";
}
forward(steps) {
return "";
}
}
Common Bugs to Avoid
Wrong: "youtube.com" instead of "facebook.com" after back(1) in canonical testNot truncating forward history on visit, so forward pointer moves incorrectly.✅ On visit, slice history list to current index + 1 before appending new URL.
Wrong: "page1.com" instead of "startpage.com" after back(1) when at startBack pointer moves below zero, allowing invalid negative index.✅ Clamp back pointer with max(0, curr - steps).
Wrong: "page2.com" instead of "page3.com" after forward(1) following visitForward history not cleared after visit following back.✅ Truncate forward history on visit by slicing history list to curr + 1.
Wrong: Timeout or TLE on large inputUsing list slicing or linear operations on visit causing O(n) per visit.✅ Use doubly linked list or two stacks to achieve O(1) amortized visit, back, and forward.