Design Browser History
🧭 What is browser history? It is what makes the back and forward buttons of a browser tab work. Every page you open is remembered in order. Back takes you to the page before, forward takes you to the page you just came back from, and opening a new link from the middle of the list throws away the pages you could have gone forward to.
Browser history is a short problem with a classic trap. Almost everyone reaches for a stack, and almost everyone then discovers that a single stack cannot go forward. The interview tests whether you can model the rule precisely ("a new visit clears the forward pages"), pick a structure that makes every button cheap, and explain why. The same shape shows up again in undo/redo, music players and text editors, so it is worth knowing well.
Requirements
"Design the history of a single browser tab. It starts on a homepage. Users can visit a URL, go back some number of steps, or go forward some number of steps."
The prompt sounds complete, but the interesting rules are in the corners. Ask about them.
Clarifying Questions
You: If I go back two pages and then visit a new URL, can I still go forward to the pages I left?
Interviewer: No. Visiting a new page clears all the forward history. That is how real browsers behave.
You: What if someone asks to go back more steps than there are pages?
Interviewer: Go back as far as possible and stop at the first page. The same for forward: stop at the newest page.
You: What do back and forward return?
Interviewer: The URL of the page you end up on. Visit returns nothing.
You: If I visit the same URL as the current page, is that a new entry?
Interviewer: Yes. Treat every visit as a new entry; don't try to merge them.
You: Is there one tab or many? And is there a limit on how long the history can be?
Interviewer: One tab, no limit for now. We may talk about both later.
You: Do we need the full "History" menu, every page ever visited with times?
Interviewer: No. Only what the back and forward buttons need.
The first answer is the heart of the problem. It means the history is not just a list that grows; it is a list with a position in it, and a visit from the middle cuts the list at that position. The second answer means back and forward never fail. They clamp, and they always return a page.
💡 Interview tip: Draw the history as a row of boxes with an arrow under the current page, then act out "visit, visit, back, visit" with the interviewer. Thirty seconds of drawing settles the forward-clearing rule better than any amount of talking, and it shows you exactly which structure you need.
Final Requirements
- The tab starts on a homepage, which is the first entry of its history.
visit(url)opensurlfrom the current page. It becomes the current page, and every page that was forward of the old current page is discarded.back(steps)moves back up tostepspages, stopping at the first page, and returns the URL of the current page after the move.forward(steps)moves forward up tostepspages, stopping at the newest page, and returns the URL of the current page after the move.- Visiting the same URL twice in a row creates two entries.
- All three operations should be fast, independent of how long the history is.
Out of scope
- Several tabs, and history shared between them (we come back to this in Extensibility).
- A size limit on the history.
- The global "History" page with every visit and its time.
- Page content, titles and scroll positions.