Your phone browser has one tab, with a back button and a forward button. Build the page history behind these buttons. The browser opens on a first page called homepage.
Write a class BrowserHistory with these methods:
BrowserHistory(homepage): open the browser on homepage.
visit(url): open url from the current page. All pages that were ahead of you (the forward pages) are forgotten.
back(steps): press back steps times. If there are fewer pages behind you, stop at the first page. Return the url of the page you end on.
forward(steps): press forward steps times. If there are fewer pages ahead of you, stop at the last page. Return the url of the page you end on.
Examples
Example 1:
Input
["BrowserHistory", "visit", "visit", "visit", "back", "back", "forward", "visit", "forward", "back", "back"]
[["start.in"], ["maps.in"], ["pay.in"], ["cart.in"], [2], [1], [1], ["help.in"], [3], [2], [10]]
Output
[null, null, null, null, "maps.in", "start.in", "maps.in", null, "help.in", "start.in", "start.in"]
Explanation
Open start.in, then visit maps.in, pay.in and cart.in.
back(2) goes to maps.in. back(1) goes to start.in. forward(1) goes to maps.in.
visit("help.in") forgets pay.in and cart.in. The history is now start.in, maps.in, help.in.
forward(3) stays on help.in. back(2) goes to start.in. back(10) stays on start.in.
Example 2:
Input
["BrowserHistory", "back", "visit", "forward", "back"]
[["x.in"], [4], ["y.in"], [2], [1]]
Output
[null, "x.in", null, "y.in", "x.in"]
Explanation
back(4) can not go anywhere, so you stay on x.in.
visit("y.in") opens a new page. forward(2) has nothing ahead, so you stay on y.in.
back(1) goes to x.in.