The problem. Design the history of one browser tab. BrowserHistory(homepage) starts on the homepage. visit(url) goes to a new page and clears all forward history. back(steps) moves back up to steps pages and forward(steps) forward up to steps pages — stopping early if history runs out — and both return the page you end up on.
BrowserHistory("leetcode.com")
visit("google.com"), visit("facebook.com"), visit("youtube.com")
back(1) -> "facebook.com"
back(1) -> "google.com"
forward(1) -> "facebook.com"
visit("linkedin.com") (drops youtube.com)
forward(2) -> "linkedin.com" (nothing ahead)
back(2) -> "google.com"
back(7) -> "leetcode.com" (stops at the homepage)Members only · free account
The rest of this lesson, with 7 more sections, 1 drawing, a dry-run simulator and code in JavaScript, Python, Java and C++, opens with a free account.
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson