Design Browser History (LeetCode 1472): Solution and Explanation
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
- Start with
history = [homepage],cur = 0,last = 0. visit(url): incrementcur. Ifcurequals the array length, appendurl; otherwise overwritehistory[cur]. Then setlast = cur.back(steps): setcur = max(0, cur - steps)and returnhistory[cur].forward(steps): setcur = min(last, cur + steps)and returnhistory[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
- Three visits give history = [leetcode, google, facebook, youtube], cur = last = 3.
- back(1) moves cur to 2 (facebook), back(1) to 1 (google), forward(1) to 2 (facebook).
- visit(linkedin): cur = 3 overwrites youtube, and last = 3.
- forward(2): min(3, 5) = 3, stay on linkedin. back(2): cur = 1, google.
- 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]1class BrowserHistory {
2 private List<String> history;
3 private int cur;
4 private int last;
5
6 public BrowserHistory(String homepage) {
7 history = new ArrayList<>();
8 history.add(homepage);
9 cur = 0;
10 last = 0;
11 }
12
13 public void visit(String url) {
14 cur++;
15 if (cur == history.size()) {
16 history.add(url);
17 } else {
18 history.set(cur, url);
19 }
20 last = cur;
21 }
22
23 public String back(int steps) {
24 cur = Math.max(0, cur - steps);
25 return history.get(cur);
26 }
27
28 public String forward(int steps) {
29 cur = Math.min(last, cur + steps);
30 return history.get(cur);
31 }
32}1class BrowserHistory {
2 vector<string> history;
3 int cur;
4 int last;
5
6public:
7 BrowserHistory(string homepage) {
8 history.push_back(homepage);
9 cur = 0;
10 last = 0;
11 }
12
13 void visit(string url) {
14 cur++;
15 if (cur == (int) history.size()) {
16 history.push_back(url);
17 } else {
18 history[cur] = url;
19 }
20 last = cur;
21 }
22
23 string back(int steps) {
24 cur = max(0, cur - steps);
25 return history[cur];
26 }
27
28 string forward(int steps) {
29 cur = min(last, cur + steps);
30 return history[cur];
31 }
32};Time and space complexity
O(1) per call
Each call does a constant number of index updates and at most one array write.
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).