All questions
Medium2026-08-09

Implement Browser History Navigation

Company
Amazon
Role

SDE-2

Round

DSA Round

DesignStackDoubly Linked List

Problem Statement

Design a BrowserHistory class that simulates browser navigation:

  1. visit(url) — Visit a new URL. Clears all forward history.
  2. back(steps) — Go back steps in history. If steps exceed available history, go to the earliest page. Return current URL.
  3. forward(steps) — Go forward steps in history. If steps exceed available forward history, go to the most recent page. Return current URL.

Constraints

  • 1 <= url.length <= 20
  • 1 <= steps <= 100
  • At most 5000 calls to visit, back, forward
  • All operations should be O(1) or O(steps)

Example

BrowserHistory history = new BrowserHistory("google.com");
history.visit("facebook.com");  // stack: [google, facebook*]
history.visit("youtube.com");   // stack: [google, facebook, youtube*]
history.back(1);                // returns "facebook.com"
history.back(1);                // returns "google.com"
history.forward(1);             // returns "facebook.com"
history.visit("linkedin.com");  // stack: [google, facebook, linkedin*] — youtube is gone
history.forward(2);             // returns "linkedin.com" (can't go forward)
history.back(2);                // returns "google.com"

What the Interviewer Expects

  1. Two approaches: Two stacks (back stack + forward stack) OR doubly linked list with a current pointer.
  2. visit() clears forward history — this is the key edge case.
  3. Clamping — back/forward should not go beyond boundaries.
  4. Clean interface — separate the data structure logic from the navigation logic.

Follow-ups

  1. How would you persist this history to survive browser restarts?
  2. How would you implement "recently closed tabs" with undo?
  3. What if you need to support multiple tabs, each with their own history?
  4. How would you limit memory usage if the user visits 100K+ pages?
🧠

No solution provided

Think through it. That's how you build real interview muscle.

Share: