Design Browser History (LeetCode 1472): Solution and Explanation

Medium
Array
Linked List
Stack
Design
Doubly Linked List
Data Stream

Build the history of a single browser tab with visit, back(steps) and forward(steps), where visiting a page wipes the forward history.

Intuition

A browser tab's history is a line of pages with a marker on the current one. Back and forward only slide the marker; they never change the pages.

If all pages live in one array, sliding the marker is index arithmetic, so back(steps) and forward(steps) cost O(1) instead of popping one page at a time.

visit is the only call that changes the line: it writes the new page right after the current one and makes everything beyond it unreachable. Deleting those pages is unnecessary. A second index, last, records where the reachable history ends, and later visits simply overwrite the stale slots.

Approach: Array with cur and last

  1. Start with history = [homepage], cur = 0, last = 0.
  2. visit(url): increment cur. If cur equals the array length, append url; otherwise overwrite history[cur]. Then set last = cur.
  3. back(steps): set cur = max(0, cur - steps) and return history[cur].
  4. forward(steps): set cur = min(last, cur + steps) and return history[cur].

Step-by-step example

homepage leetcode.com; visit google, facebook, youtube; back 1, back 1, forward 1; visit linkedin; forward 2, back 2, back 7
  1. Three visits give history = [leetcode, google, facebook, youtube], cur = last = 3.
  2. back(1) moves cur to 2 (facebook), back(1) to 1 (google), forward(1) to 2 (facebook).
  3. visit(linkedin): cur = 3 overwrites youtube, and last = 3.
  4. forward(2): min(3, 5) = 3, stay on linkedin. back(2): cur = 1, google.
  5. back(7): max(0, -6) = 0, leetcode.

Code: Python, Java, C++

1class BrowserHistory:
2    def __init__(self, homepage: str):
3        self.history = [homepage]
4        self.cur = 0
5        self.last = 0
6
7    def visit(self, url: str) -> None:
8        self.cur += 1
9        if self.cur == len(self.history):
10            self.history.append(url)
11        else:
12            self.history[self.cur] = url
13        self.last = self.cur
14
15    def back(self, steps: int) -> str:
16        self.cur = max(0, self.cur - steps)
17        return self.history[self.cur]
18
19    def forward(self, steps: int) -> str:
20        self.cur = min(self.last, self.cur + steps)
21        return self.history[self.cur]
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(1) per call

Each call does a constant number of index updates and at most one array write.

Space

O(n)

The array holds at most one slot per visit, including stale slots that are reused later.

Other approaches

Two stacks (time O(steps) per back or forward, space O(n))

Keep a back stack and a forward stack. visit pushes onto back and clears forward; back and forward move pages between stacks one at a time.

Doubly linked list (time O(steps) per back or forward, space O(n))

Each page is a node with prev and next links. visit cuts the next link and attaches a new node. Moving costs O(steps).

Common mistakes

  • Using the array length as the forward limit. After a visit in the middle, the slots past last are stale and must not be reachable.
  • Truncating the array on every visit. It is correct, but in some languages it is O(n) work that the last index makes unnecessary.
  • Forgetting to clamp. back and forward must stop at index 0 and at last, not throw or wrap around.

Frequently asked questions

Why keep stale pages instead of deleting them?

They are never read, because forward is clamped by last. The next visit overwrites them, so keeping them costs nothing and avoids resizing.

Can the same url appear more than once?

Yes. Every visit adds a new slot, even for a page already in the history, just like a real browser.

Is the two-stack solution accepted?

Yes. Steps are at most 100 and there are at most 5000 calls, so O(steps) moves are fast enough. The array version is simply O(1).