Design Browser History

Medium

Topics
ArrayLinked ListStackDesign

You have a browser with one tab starting on a homepage. Implement BrowserHistory(homepage), visit(url) (visits url from the current page and clears all forward history), back(steps) (move back up to steps pages, return the current url), and forward(steps) (move forward up to steps pages, return the current url).

Example 1

returnsBrowserHistory(leetcode)visit(google)visit(facebook)visit(youtube)back(1)facebookback(1)googleforward(1)facebookvisit(linkedin)forward(2)linkedinback(2)googleback(7)leetcode
One array and a cursor is enough. The rule that carries the behaviour is that visit truncates everything ahead of the cursor — the forward history is gone once you navigate somewhere new — while back and forward only move the cursor and clamp at the ends.
Calls run top to bottom; the arrow shows what each one returns.
Input:  operations = ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"], values = [["leetcode"],["google"],["facebook"],["youtube"],[1],[1],[1],["linkedin"],[2],[2],[7]]
Output: [null,null,null,null,facebook,google,facebook,null,linkedin,google,leetcode]

Constraints

  • 1 <= homepage.length, url.length <= 20
  • 1 <= steps <= 100
  • At most 5000 calls.
Editorial — intuition, approach, complexity and pitfalls

Intuition

History is a line of pages with a cursor at the current one. Store the visited pages in a list and an index; visiting truncates everything after the cursor, back/forward just clamp the cursor.

Approach

  1. Keep a list of urls and a current index, starting with the homepage.
  2. visit truncates the list after the current index, appends the url, and advances the index.
  3. back/forward move the index by up to steps but clamp to the valid range, returning the url there.

Dry Run

BrowserHistory("leetcode")
  visit google     at=1   pages [leetcode, google]
  visit facebook   at=2   pages [leetcode, google, facebook]
  visit youtube    at=3   pages [leetcode, google, facebook, youtube]
  back 1     -> facebook    at=2
  back 1     -> google      at=1
  forward 1  -> facebook    at=2
  visit linkedin   at=3   pages [leetcode, google, facebook, linkedin]
             -- youtube is DISCARDED; visiting from a back position truncates
  forward 2  -> linkedin    at=3   clamped: there is nothing ahead
  back 2     -> google      at=1
  back 7     -> leetcode    at=0   clamped at the homepage
answer null|null|null|facebook|google|facebook|null|linkedin|google|leetcode
The truncation at `visit linkedin` is the only non-obvious line, and the trace
shows its consequence two rows later. Without `del pages[at+1:]`, youtube would
survive at index 3 and linkedin would land at index 4, so `back 2` would return
facebook instead of google -- a stale forward page resurfacing in the middle of
the list.
Both back and forward clamp with max and min rather than validating their input,
which is why `back 7` on a 4-page list is not an error. The problem permits more
steps than exist and defines the result as going as far as possible.
A single array plus a cursor handles all of this. Two stacks also work, but then
"visit" has to explicitly empty the forward stack, which is the same truncation
spelled differently.

Complexity

  • Time — O(1) amortized: back/forward are constant; visit is amortized constant.
  • Space — O(n): stores every page on the current history line.

Common Pitfalls

  • visit must discard forward history, otherwise stale forward pages resurface.

Full Solution — all languages

class BrowserHistory:
    def __init__(self, homepage):
        self.pages = [homepage]
        self.at = 0

    def visit(self, url):
        # everything ahead of the current page is discarded
        del self.pages[self.at + 1:]
        self.pages.append(url)
        self.at += 1

    def back(self, steps):
        self.at = max(0, self.at - steps)
        return self.pages[self.at]

    def forward(self, steps):
        self.at = min(len(self.pages) - 1, self.at + steps)
        return self.pages[self.at]
Asked at Roblox
SensAI
Beta
Tap the mic to stop voice mode