Editor

Browser History

#300 · Classes · Intermediate · 70 XP · about 14 min

A browser tab remembers the pages you have seen so you can go back and forward. Build a BrowserHistory class:

  • BrowserHistory(homepage) opens a new tab on homepage.
  • visit(url) opens url from the current page. Every page you could have reached with forward is forgotten.
  • back(steps) goes back up to steps pages, stopping at the first page of the tab, and returns the URL you end up on.
  • forward(steps) goes forward up to steps pages, stopping at the newest page, and returns the URL you end up on.
  • current() returns the URL you are on.

Examples

tab = BrowserHistory('news.com')
tab.visit('sport.com')
tab.visit('scores.com')
tab.back(1)              # returns 'sport.com'
tab.back(5)              # returns 'news.com'     (it cannot go further back)
tab.forward(1)           # returns 'sport.com'
tab.visit('weather.com')
tab.forward(1)           # returns 'weather.com'  (scores.com is gone)
tab.back(1)              # returns 'sport.com'

Constraints

  • Up to 50,000 calls; URLs are non-empty strings
  • 0 <= steps <= 10^9, so moving one page at a time for every step is far too slow

Note

The same URL can appear several times in one history: every visit is a separate entry.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.