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 onhomepage.visit(url)navigates tourlfrom the current page and clears all forward history.back(steps)moves back up tostepspages, stopping at the oldest page, and returns the current URL afterwards.forward(steps)moves forward up tostepspages, 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
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 2 visible tests, Submit all 7. ⌘Enter runs, ⌘⇧Enter submits.