Skip to content
Onsite

Design Browser History

MediumP2Coding

Design the back and forward behavior of a browser tab. The tab starts on a homepage.

Implement the BrowserHistory class:

  • BrowserHistory(homepage) starts on homepage.
  • visit(url) navigates to url from the current page and clears all forward history.
  • back(steps) moves back up to steps pages, stopping at the oldest page, and returns the current URL afterwards.
  • forward(steps) moves forward up to steps pages, stopping at the newest page, and returns the current URL afterwards.

Moving back and forward does not remove pages; only visit discards the pages ahead of the current one.

Example 1

Input
["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["a.com"],["b.com"],["c.com"],["d.com"],[1],[1],[1],["e.com"],[2],[2],[7]]
Output
[null,null,null,null,"c.com","b.com","c.com",null,"e.com","b.com","a.com"]
Why
Visiting e.com after going back erases the forward pages (d.com).

Constraints

  • 1 <= steps <= 100
  • 1 <= url.length <= 20
  • At most 5000 calls.

Hints

Run checks the 2 visible tests, Submit all 7.