Design Browser History

MediumDesigndesign

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Model a single browser tab's back/forward navigation. Implement: - BrowserHistory(homepage) opens the tab on homepage. - visit(url) navigates to url from the current page. This **discards all forward history**. - back(steps) moves back up to steps pages (stopping at the oldest page) and returns the current URL afterwards. - forward(steps) moves forward up to steps pages (stopping at the newest page) and returns the current URL afterwards. Visiting a URL equal to the current page still adds a new entry. Aim for O(1) per operation (amortized for visit).

Examples

Input: ["BrowserHistory","visit","visit","back","back","forward","visit","forward","back"] [["home.com"],["a.com"],["b.com"],[1],[5],[1],["c.com"],[2],[2]]
Output: [null,null,null,"a.com","home.com","a.com",null,"c.com","home.com"]
History is home, a, b. Back 1 reaches a.com; back 5 stops at home.com; forward 1 returns to a.com. Visiting c.com drops b.com, so forward stays on c.com and back 2 reaches home.com.
Input: ["BrowserHistory","forward","back"] [["x.io"],[3],[1]]
Output: [null,"x.io","x.io"]
With only the homepage there is nowhere to go in either direction.

Constraints

  • 1 <= homepage.length, url.length <= 20
  • 1 <= steps <= 100
  • URLs contain lowercase letters, digits and `.`
  • At most 5000 calls in total.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More design problems