Design Browser History
Medium
Topics
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
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
- Keep a list of urls and a current index, starting with the homepage.
- visit truncates the list after the current index, appends the url, and advances the index.
- 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]import java.util.*;
class BrowserHistory {
private final List<String> pages = new ArrayList<>();
private int at = 0;
public BrowserHistory(String homepage) {
pages.add(homepage);
}
public void visit(String url) {
while (pages.size() > at + 1) pages.remove(pages.size() - 1);
pages.add(url);
at++;
}
public String back(int steps) {
at = Math.max(0, at - steps);
return pages.get(at);
}
public String forward(int steps) {
at = Math.min(pages.size() - 1, at + steps);
return pages.get(at);
}
}#include <bits/stdc++.h>
using namespace std;
class BrowserHistory {
public: BrowserHistory(string homepage) : at_(0) {
pages_.push_back(homepage);
}
void visit(string url) {
pages_.resize(at_ + 1);
pages_.push_back(url);
at_++;
}
string back(int steps) {
at_ = max(0, at_ - steps);
return pages_[at_];
}
string forward(int steps) {
at_ = min((int)pages_.size() - 1, at_ + steps);
return pages_[at_];
}
private: vector<string> pages_;
int at_;
};class BrowserHistory {
constructor(homepage) {
this.pages = [homepage];
this.at = 0;
}
visit(url) {
this.pages.length = this.at + 1;
this.pages.push(url);
this.at++;
}
back(steps) {
this.at = Math.max(0, this.at - steps);
return this.pages[this.at];
}
forward(steps) {
this.at = Math.min(this.pages.length - 1, this.at + steps);
return this.pages[this.at];
}
}
Asked at
Roblox