Skip to the code
cf.
+25 XP

#246 Tab History Navigator

medium

Build the back and forward buttons of a single browser tab. Write a class TabHistory:

  • new TabHistory(homepage) opens the tab on homepage.
  • visit(url) goes to url from the current page. Any pages you could have gone forward to are forgotten.
  • back(steps) moves back up to steps pages (it stops at the first page) and returns the current URL.
  • forward(steps) moves forward up to steps pages (it stops at the newest page) and returns the current URL.

Each example lists the calls in order, then each call's arguments. The first call creates the object with new TabHistory(…); the output lists what each call returns (null for the constructor and for methods that return nothing).

Example 1

Input: ["TabHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"]
[["home.dev"],["docs.dev"],["blog.dev"],["shop.dev"],[1],[1],[1],["help.dev"],[2],[2],[7]]
Output: [null,null,null,null,"blog.dev","docs.dev","blog.dev",null,"help.dev","docs.dev","home.dev"]

After going back twice to docs.dev and forward once to blog.dev, visiting help.dev forgets shop.dev, so forward(2) stays on help.dev.

Example 2

Input: ["TabHistory","back","forward"]
[["start.io"],[3],[3]]
Output: [null,"start.io","start.io"]

With only the homepage, both buttons stay on start.io.

Constraints

  • 1 <= steps <= 100
  • URLs are short strings of lowercase letters and dots.
  • At most 5000 calls in total.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).