1472. Design Browser History - Detailed Explanation
Problem Statement
You need to implement a browser history manager supporting three operations on strings representing URLs:
- visit(url): navigate to
urlfrom the current page. This clears any “forward” history. - back(steps): move back up to
stepspages; return the current page after moving. - forward(steps): move forward up to
stepspages; return the current page.
Examples
Example 1
Input:
BrowserHistory bh = new BrowserHistory("leetcode.com");
bh.visit("google.com");
bh.visit("facebook.com");
bh.visit("youtube.com");
bh.back(1); // returns "facebook.com"
bh.back(1); // returns "google.com"
bh.forward(1); // returns "facebook.com"
bh.visit("linkedin.com");
bh.forward(2); // returns "linkedin.com" (no forward history)
bh.back(2); // returns "google.com"
bh.back(7); // returns "leetcode.com"
Explanation
- Start at “leetcode.com”
- Visits build history: [leetcode→google→facebook→youtube], current=“youtube”
back(1)→ “facebook”back(1)→ “google”forward(1)→ “facebook”visit("linkedin.com")wipes forward to yield [leetcode→google→facebook→linkedin], current=“linkedin”forward(2)can’t go forward → stays “linkedin”back(2)→ “google”back(7)→ hits oldest “leetcode”
Example 2
Input:
BrowserHistory bh = new BrowserHistory("a.com");
bh.visit("b.com");
bh.back(1); // "a.com"
bh.visit("c.com");
bh.forward(1); // "c.com"
Constraints
- All URLs are non‑empty strings of length ≤ 20, consisting of letters, digits, ‘.’ and ‘/’.
- Total calls ≤ 5 × 10⁴.
- 1 ≤ steps ≤ 100.
Hints
- How would you implement this with an array/list and a current‑index pointer?
- How do you “clear forward history” when visiting a new URL?
- Can two stacks (back and forward) give O(1) for each operation?
- Could a doubly linked list with a current pointer work too?
Approach 1 – ArrayList + Current Index
Idea
Keep a resizable array history[] and an integer curIdx.
- visit(url): truncate
historyto lengthcurIdx+1, appendurl, setcurIdx = end. - back(steps):
curIdx = max(0, curIdx - steps); returnhistory[curIdx]. - forward(steps):
curIdx = min(history.size()-1, curIdx + steps); returnhistory[curIdx].
Walkthrough
Using Example 1:
- init:
history=["leetcode.com"],curIdx=0 - visit “google.com”: truncate to [0], append →
["leetcode","google"],curIdx=1 - visit “facebook.com”: →
["leetcode","google","facebook"],curIdx=2 - visit “youtube.com”: →
["leetcode","google","facebook","youtube"],curIdx=3 - back(1):
curIdx=2, return"facebook" - back(1):
curIdx=1, return"google" - forward(1):
curIdx=2, return"facebook" - visit “linkedin.com”: truncate to idx=2 → keep
["leetcode","google","facebook"], append →["leetcode","google","facebook","linkedin"],curIdx=3 - forward(2):
curIdx = min(3,3+2)=3, return"linkedin" - back(2):
curIdx=1, return"google" - back(7):
curIdx=0, return"leetcode"
Complexity
- Time: All ops O(1) amortized for index math and list append/truncate.
- Space: O(N) for history, N ≤ number of visits.
Python Code
Python3
Python3
. . . .
Java Code
Java
Java
. . . .
Approach 2 – Two Stacks
Idea
Maintain two stacks:
- backStack holds pages left of current.
- forwardStack holds pages right of current.
Keep a variablecurrent. - visit(url): push
currentonto backStack, setcurrent=url, clear forwardStack. - back(steps):
- repeat
stepstimes: if backStack empty break; pushcurrentonto forwardStack, pop backStack intocurrent. - return
current.
- repeat
- forward(steps) symmetrically.
Complexity
- Time: O(steps) for back/forward, which ≤ 100 per call. All ops worst‑case O(100).
- Space: O(N) across two stacks.
Common Mistakes
- Forgetting to clear forward history on
visit, leading to incorrect “forward” behavior. - Off‑by‑one when updating
currentvs. pushing onto stacks. - Not handling attempts to go back/forward beyond available history.
Edge Cases
- Many
backcalls at start → should stay on earliest page. forwardimmediately after initialization (no forward) → remain on homepage.- Repeated
visitwithout any navigation.
Alternative Variations
- Limit total history size and evict oldest entries.
- Store timestamps or visit counts.
- Support bookmarks (random access) in O(1).
Related Problems
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-
GET YOUR FREE
Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
135. Candy - Detailed Explanation
Learn to solve Leetcode 135. Candy with multiple approaches.
85. Maximal Rectangle - Detailed Explanation
Learn to solve Leetcode 85. Maximal Rectangle with multiple approaches.
1348. Tweet Counts Per Frequency - Detailed Explanation
Learn to solve Leetcode 1348. Tweet Counts Per Frequency with multiple approaches.
2684. Maximum Number of Moves in a Grid - Detailed Explanation
Learn to solve Leetcode 2684. Maximum Number of Moves in a Grid with multiple approaches.
305. Number of Islands II - Detailed Explanation
Learn to solve Leetcode 305. Number of Islands II with multiple approaches.
2851. String Transformation - Detailed Explanation
Learn to solve Leetcode 2851. String Transformation with multiple approaches.
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
4.6
$197

Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
$78
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
$78
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.