Design Browser History
Keep a browser's back and forward history in one list, with two indexes that make every call a single step.
Write a class that keeps the history of one browser tab. The constructor takes the homepage, which is the page shown at the start. The class has three methods:
opensvisit(url) from the current page and forgets every page you could have reached with forward.url goes back up toback(steps) pages, stopping at the homepage, and returns the page now shown.steps goes forward up toforward(steps) pages, stopping at the newest page that has not been forgotten, and returns the page now shown.steps
Example 1
Output: "maps.example", "news.example", "maps.example", "shop.example", "news.example", "home.example"
The output lists only the values that backforwardvisit
The Idea
Every web browser has a back button and a forward button. Back returns to the page you were on before, and forward undoes a back. This class keeps the list of pages behind those two buttons.
Follow Example 1. The tab starts on home.examplenews.examplemaps.examplevideo.examplevideo.exampleback(1)maps.exampleback(1)news.exampleforward(1)maps.examplevideo.example
Then comes visit("shop.example")maps.examplevideo.exampleforward(2)shop.example
Keep the pages in one list, in the order they were reached. Number the places in the list from 0 and call each one a slot. A number named curcurlast
In the top row, labelled after back(1), back(1), forward(1)curmaps.examplelastvideo.examplecurshop.examplevideo.examplelast
So a visit never has to delete pages. It writes into the slot after curlastlast
Solution Approach
With the list, curlastcur
Step counts that go too far
What should back(7)steps
One line of arithmetic in each method gives the same slot at once. The method backcurMath.max(0, cur - steps)forwardMath.min(last, cur + steps)back(7)Math.max(0, 1 - 7)home.example
Forgetting the pages ahead
Which pages should forward reach after a visit? Only the pages up to the new one. The direct way is to delete every slot after curpages.subList(cur + 1, pages.size()).clear()
Moving lastlastlast
Two piles instead of one list
Could two piles of pages do the same job? In the top row of the picture, one pile would hold home.examplenews.examplenews.exampleback(1)video.exampleforward(1)back(1)
That design is correct too: back moves pages from the first stack to the second, forward moves them the other way, and a visit empties the forward stack. But back(7)curlast
Java Solution
The class DesignBrowserHistory
: the list of slots, anpages that starts with the homepage in slot 0.ArrayList : the slot of the page on screen. The methodscur andback move it withforward andMath.max and return the page in that slot.Math.min : the slot of the newest page thatlast may reach.forward : movesvisit one slot on and writes the URL there, withcur when the slot already exists andset when the list must grow. Then it setsadd tolast .cur
The mainforward(5)back(5)last
import java.util.ArrayList;
import java.util.List;
public class DesignBrowserHistory {
private final List<String> pages = new ArrayList<>();
private int cur = 0; // slot of the page being shown
private int last = 0; // slot of the newest page that forward may reach
public DesignBrowserHistory(String homepage) {
pages.add(homepage);
}
public void visit(String url) {
cur++;
if (cur < pages.size()) {
pages.set(cur, url); // reuse the slot of an old forward page
} else {
pages.add(url); // the list is full: grow it by one
}
last = cur; // pages after the new one are no longer reachable
}
public String back(int steps) {
cur = Math.max(0, cur - steps); // never before the homepage
return pages.get(cur);
}
public String forward(int steps) {
cur = Math.min(last, cur + steps); // never past the newest reachable page
return pages.get(cur);
}
public static void main(String[] args) {
DesignBrowserHistory h = new DesignBrowserHistory("home.example");
h.visit("news.example");
h.visit("maps.example");
h.visit("video.example");
System.out.println("back(1) -> " + h.back(1));
System.out.println("back(1) -> " + h.back(1));
System.out.println("forward(1) -> " + h.forward(1));
h.visit("shop.example");
System.out.println("forward(2) -> " + h.forward(2));
System.out.println("back(2) -> " + h.back(2));
System.out.println("back(7) -> " + h.back(7));
DesignBrowserHistory fresh = new DesignBrowserHistory("home.example");
System.out.println("fresh: forward(5) -> " + fresh.forward(5)
+ ", back(5) -> " + fresh.back(5));
DesignBrowserHistory cut = new DesignBrowserHistory("home.example");
cut.visit("news.example");
cut.visit("maps.example");
cut.visit("video.example");
System.out.println("cut: back(2) -> " + cut.back(2));
cut.visit("shop.example");
System.out.println("cut: forward(1) -> " + cut.forward(1)
+ ", list still holds " + cut.pages);
}
}
Running it prints:
back(1) -> maps.example
back(1) -> news.example
forward(1) -> maps.example
forward(2) -> shop.example
back(2) -> news.example
back(7) -> home.example
fresh: forward(5) -> home.example, back(5) -> home.example
cut: back(2) -> news.example
cut: forward(1) -> shop.example, list still holds [home.example, news.example, shop.example, video.example]
The six lines from back(1) -> maps.exampleback(7) -> home.exampleforward(2) -> shop.examplecurfresh:home.examplecurlast
Now look at the last line. After back(2)video.exampleshop.examplevideo.exampleforward(1)shop.examplelastforwardpages.size() - 1forward(1) -> video.example
Complexity
Each call does a fixed amount of work, however many pages the history holds and however large steps
- Time: O(1) per call, because
andback compute the new slot with oneforward orMath.max and read one slot, andMath.min writes one slot. Now and thenvisit has to make room for more slots, but theadd documentation promises that this averages out to constant time per add.ArrayList - Space: O(v) for v calls to visit, because the list holds the homepage plus at most one new slot per visit, and dropped pages keep their slots until a later visit writes over them.
For a class that is built on a stack, see