Design Browser History solution explained

Design Browser History asks for a single tab's history with visit, back and forward, where visiting a new page throws away every page you could have gone forward to. The cleanest answer is one array plus two integers: the current position and the last valid position. Back and forward become clamped index moves, and visit overwrites the slot after the current one, so every operation runs in O(1) time, with visit amortized.

By Published 8 min read

The task, restated

A browser tab starts on a home page. visit(url) opens url from the current page, and after that there is nothing to go forward to, even if there was before. back(steps) moves back up to steps pages, stopping at the home page if steps is too large, and returns the page you land on. forward(steps) does the same in the other direction, stopping at the newest page that is still valid. The interviewer wants all three operations to be fast, and typically there are many calls.

A made-up session starting on start.page
CallReturnsHistory (current in caps)
visit(news)start, NEWS
visit(maps)start, news, MAPS
visit(mail)start, news, maps, MAIL
back(1)mapsstart, news, MAPS, mail
back(1)newsstart, NEWS, maps, mail
forward(1)mapsstart, news, MAPS, mail
visit(docs)start, news, maps, DOCS; mail is gone
forward(2)docsnothing newer than docs
back(2)newsstart, NEWS, maps, docs
back(7)startclamped at the home page
A made-up session starting on start.page. Scroll sideways on a small screen.

The visit(docs) row is the whole problem. Before it, mail was reachable with forward. After it, mail must never come back, even though forward(2) asks for more steps than exist. Ask the interviewer whether visiting the page you are already on counts as a new entry; in the usual version it does.

What it is really testing

This is a light design question, often used early in a loop or as a phone screen. There is no clever algorithm. The interviewer is looking at how you pick a representation, whether you clamp the step counts correctly, and whether you notice that deleting forward history does not require deleting anything. It also opens doors to follow-ups:

  • Cap the history at N pages. A ring buffer of size N, as in Design Circular Queue, drops the oldest page when full.
  • Support many tabs. One history object per tab, stored in a map from tab id.
  • Show the back list, like a long-press on the back button. Return a slice of the array from cur - 1 down to 0.
  • Persist across restarts. Store the array and the two indexes; the design serializes trivially.

Slow but correct: slice on every visit

Keep a list of pages and the index of the current one. Back and forward clamp the index. Visit cuts the list right after the current page and appends the new url. Cutting a Python list or a JavaScript array this way copies the kept part, so visit costs O(n) where n is the history length. That is acceptable for a toy but not for many calls on a long history, which is why it makes a good opening line and a good test oracle.

class SlicingHistory:
    def __init__(self, homepage):
        self.pages = [homepage]
        self.cur = 0

    def visit(self, url):
        self.pages = self.pages[:self.cur + 1] + [url]
        self.cur += 1

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

    def forward(self, steps):
        self.cur = min(len(self.pages) - 1, self.cur + steps)
        return self.pages[self.cur]
Python: correct, with an O(n) copy on each visit.

A common middle answer: two stacks

Many candidates reach for two stacks, the way the real buttons feel. The back stack holds pages behind you, with the current page on top; the forward stack holds pages ahead of you. Back pops from the back stack and pushes onto the forward stack, one page at a time, never popping the last remaining page. Forward does the reverse. Visit pushes onto the back stack and empties the forward stack. Moves cost O(steps), visit is O(1) if you replace the forward stack instead of popping it item by item. It is a solid answer; the array version below simply removes the step loop.

Optimal approach: one array, two indexes

Keep pages in an array, cur as the index of the current page, and last as the index of the newest page that is still valid. Back sets cur to max(0, cur - steps). Forward sets cur to min(last, cur + steps). Visit moves cur forward by one, writes the url there (appending if the array is not long enough yet, overwriting otherwise), and sets last to cur. Any stale pages beyond last stay in memory but can never be reached, because forward is clamped to last. They are overwritten the next time history grows that far.

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

    def visit(self, url):
        self.cur += 1
        if self.cur == len(self.pages):
            self.pages.append(url)
        else:
            self.pages[self.cur] = url
        self.last = self.cur

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

    def forward(self, steps):
        self.cur = min(self.last, self.cur + steps)
        return self.pages[self.cur]
Python: O(1) back and forward, amortized O(1) visit.
class BrowserHistory {
  constructor(homepage) {
    this.pages = [homepage];
    this.cur = 0;
    this.last = 0;
  }

  visit(url) {
    this.cur += 1;
    this.pages[this.cur] = url;
    this.last = this.cur;
  }

  back(steps) {
    this.cur = Math.max(0, this.cur - steps);
    return this.pages[this.cur];
  }

  forward(steps) {
    this.cur = Math.min(this.last, this.cur + steps);
    return this.pages[this.cur];
  }
}
JavaScript: the same three fields.

JavaScript arrays grow when you assign one past the end, so visit needs no append branch. Python lists raise an error on that assignment, hence the explicit check. The other detail worth saying aloud: forward clamps to last, not to the array length. Clamping to the length is the single most common bug in this problem, because it resurrects pages that visit was supposed to erase.

Trace of the key moment

Fields around visit(docs) in the session above
Afterpagescurlast
visit(mail)[start, news, maps, mail]33
back(1), back(1)[start, news, maps, mail]13
forward(1)[start, news, maps, mail]23
visit(docs)[start, news, maps, docs]33
forward(2)unchanged3, clamped to last3
Fields around visit(docs) in the session above. Scroll sideways on a small screen.

In this session visit(docs) overwrote mail because the array already had a slot at index 3. Had the user gone back two pages first, docs would land at index 2, and the old slot 3 would sit unused beyond last until history grew again.

Time and space

n pages in history, k steps requested
Designvisitback / forwardSpace
Slice the list on visitO(n)O(1)O(n)
Doubly linked list of pagesO(1)O(min(k, n))O(n) plus a node per page
Two stacksO(1)O(min(k, n))O(n)
Array with cur and lastO(1) amortizedO(1)O(largest history ever reached)
n pages in history, k steps requested. Scroll sideways on a small screen.

The array keeps stale entries, so its memory is set by the longest history the tab ever had, not its current length. If the interviewer asks, you can trim the array on visit when the stale tail is much larger than the live part, which keeps space proportional to live history at an amortized cost.

Edge cases

  • back with more steps than available returns the home page; forward with more steps than available returns the newest valid page.
  • back(0) and forward(0) return the current page and change nothing.
  • Visiting right after going back discards every page ahead, including ones that will never be overwritten.
  • The same url visited twice in a row becomes two entries, unless the interviewer says otherwise.
  • A fresh object: back and forward both return the home page.

Mistakes to avoid

  • Clamping forward to the array length instead of last, which brings erased pages back.
  • Forgetting to update last on visit.
  • Letting back pop the final page off the back stack in the two-stack version, leaving no current page.
  • Clearing the forward stack by popping each item, which makes visit O(n) for no reason.
  • Using negative step counts without asking; normally steps are positive.

How to explain it out loud

Fictional example

Making the design choice explicit
Candidate
Two stacks would work, with moves linear in the step count. I can do better with one array, a current index and a last-valid index.
Candidate
Back is max of zero and cur minus steps. Forward is min of last and cur plus steps. Visit moves cur up one, writes the url there, and sets last to cur.
Candidate
That makes forward history invisible without deleting it, and every operation is constant time. The tradeoff is that stale slots stay in memory until they are overwritten.

Design Underground System is another small design class where picking the right maps is the whole answer. Buildings With an Ocean View is a quick array scan that pairs well in a short practice session. Implement Queue Using Stacks explores the two-stack idea in more depth, and LRU Cache is the classic design question to try after this one.

A note on Natively

Natively is a desktop app for Mac and Windows, and in practice it can read a design prompt like this from a screenshot and draft a class to compare against yours. It also transcribes your spoken reasoning, which is useful here because the grading leans on how clearly you explain the last-valid index rather than on the code. The AI interview assistant page covers setup.

Questions people ask

How do you design browser history with O(1) operations?

Keep the pages in an array with two indexes: cur for the current page and last for the newest page that is still valid. Back sets cur to max(0, cur - steps), forward sets cur to min(last, cur + steps), and visit writes the url at cur + 1 and sets last to cur.

Is two stacks a good answer for Design Browser History?

It is a solid answer that matches how the buttons feel: a back stack and a forward stack, with visit clearing the forward stack. Back and forward cost O(steps), though, while the array with cur and last makes them O(1).

What is the most common bug in Design Browser History?

Clamping forward to the array length instead of to last. Stale pages beyond last stay in the array after a visit, so clamping to the length brings back pages that visit was supposed to erase.

What is the time and space complexity of the array design?

Visit is O(1) amortized, and back and forward are O(1). Memory is set by the longest history the tab ever reached, because stale slots stay until they are overwritten.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP