Design Browser History

Implement BrowserHistory for a single browser tab. The constructor opens homepage. visit(url) opens url from the current page and erases all forward history. back(steps) moves back at most steps pages and returns the current url, and forward(steps) moves forward at most steps pages and returns the current url.

30:00

Design Browser History
medium
Free written solution
Topics
Companies

Implement BrowserHistory for a single browser tab. The constructor opens homepage. visit(url) opens url from the current page and erases all forward history. back(steps) moves back at most steps pages and returns the current url, and forward(steps) moves forward at most steps pages and returns the current url.

Example 1:
Input: {"commands":["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"],"values":[["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]]}
Output: [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"]
Constraints:
  • 1≤homepage.length,url.length≤201 \leq \text{homepage.length}, \text{url.length} \leq 20

  • 1≤steps≤1001 \leq \text{steps} \leq 100

  • homepage and url consist of lowercase English letters and ..

  • At most 5000 calls will be made to visit, back, and forward.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"commands":["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"],"values":[["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]]}
new("leetcode.com")
visit("google.com")
visit("facebook.com")
visit("youtube.com")
back(1)
back(1)
forward(1)

…

Keep every page in one array with two indexes: cur is the page on screen, last is the newest page you can still go forward to.

←
→
about:blank

history array (slots after last are stale)

Returned values

[]
Variables
VariableValue
cur0
last0
DepthFunction Call
Stack empty
0/22