Skip to content

Design Browser History

MediumLinked List
Decode it first. Clues, constraints, pattern and plan before any code.

Design a class BrowserHistory for a single browser tab.

  • BrowserHistory(homepage) opens the tab on homepage.
  • visit(url) goes to url from the current page and clears all forward history.
  • back(steps) moves back up to steps pages and returns the current URL. If there are fewer pages behind you, it stops at the first page.
  • forward(steps) moves forward up to steps pages and returns the current URL, stopping at the last page.
h = BrowserHistory("home.com")
h.visit("a.com"); h.visit("b.com"); h.visit("c.com")
h.back(1)      →  "b.com"
h.back(1)      →  "a.com"
h.forward(1)   →  "b.com"
h.visit("d.com")              (c.com is gone from forward history)
h.forward(2)   →  "d.com"
h.back(2)      →  "a.com"
h.back(7)      →  "home.com"

Constraints

  • 1 <= homepage.length, url.length <= 20
  • 1 <= steps <= 100
  • At most 5000 calls to visit, back, and forward.