Skip to content
Decoder

Design Browser History

Medium

Problem

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.

Example 1

h = BrowserHistory("home.com")

Example 2

h.visit("a.com"); h.visit("b.com"); h.visit("c.com")

Example 3

Input
h.back(1)
Output
"b.com"

Example 4

Input
h.back(1)
Output
"a.com"

Example 5

Input
h.forward(1)
Output
"b.com"

Example 6

h.visit("d.com")              (c.com is gone from forward history)

Example 7

Input
h.forward(2)
Output
"d.com"

Example 8

Input
h.back(2)
Output
"a.com"

Example 9

Input
h.back(7)
Output
"home.com"

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.