Sign in to track progress, unlock prep layers, and browse your pipeline.Sign in
Implement single-tab browser history navigation
Browser Session History Navigation
Implement a BrowserSession class to manage the navigation history for a single-tab browser. The session should start at a specified homepage.
APIs:
BrowserSession(string homepage): Initializes the browser session. Thehomepageis the first page visited, and the current page starts athomepage.void visit(string url): Navigates to the givenurlfrom the current page. This action clears any existing forward history.string back(int steps): Moves the current page pointer up tostepspages backward in the history. If fewer thanstepspages are available in the backward history, move as far back as possible. Returns the URL of the page after the move.string forward(int steps): Moves the current page pointer up tostepspages forward in the history. If fewer thanstepspages are available in the forward history, move as far forward as possible. Returns the URL of the page after the move.
Bonus:
bool haveVisited(string url): Returnstrueif the session has ever visited the givenurlat least once since its construction, regardless of whether it is currently in the back or forward history. Returnsfalseotherwise.
Requirements:
- Target O(1) amortized time complexity for each operation.
- Target O(N) space complexity for
Ntotal visits. - Clearly define and justify how to handle edge cases (e.g.,
steps <= 0, visiting the same URL consecutively, an empty forward history). - Describe and justify your chosen data structure(s) (e.g., two stacks, doubly linked list with a cursor, dynamic array with an index) and discuss their trade-offs in terms of time and space complexity.
AI interview prep
Core dilemma
Efficiently managing a mutable linear history (back/forward navigation) with fast `visit` operations that truncate future history, while strictly adhering to O(1) amortized time and O(N) space complexity, and handling the `haveVisited` bonus.
Mid-level signals3
Senior signals4
Red flags4
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement single-tab browser history navigation
Browser Session History Navigation
Implement a BrowserSession class to manage the navigation history for a single-tab browser. The session should start at a specified homepage.
APIs:
BrowserSession(string homepage): Initializes the browser session. Thehomepageis the first page visited, and the current page starts athomepage.void visit(string url): Navigates to the givenurlfrom the current page. This action clears any existing forward history.string back(int steps): Moves the current page pointer up tostepspages backward in the history. If fewer thanstepspages are available in the backward history, move as far back as possible. Returns the URL of the page after the move.string forward(int steps): Moves the current page pointer up tostepspages forward in the history. If fewer thanstepspages are available in the forward history, move as far forward as possible. Returns the URL of the page after the move.
Bonus:
bool haveVisited(string url): Returnstrueif the session has ever visited the givenurlat least once since its construction, regardless of whether it is currently in the back or forward history. Returnsfalseotherwise.
Requirements:
- Target O(1) amortized time complexity for each operation.
- Target O(N) space complexity for
Ntotal visits. - Clearly define and justify how to handle edge cases (e.g.,
steps <= 0, visiting the same URL consecutively, an empty forward history). - Describe and justify your chosen data structure(s) (e.g., two stacks, doubly linked list with a cursor, dynamic array with an index) and discuss their trade-offs in terms of time and space complexity.