59. create a browser history

medium  - accepted / - tried

I believe you are very familiar about your browser you are currently visiting https://bigfrontend.dev with.

The common actions relating to history are:

  1. new BrowserHistory() - when you open a new tab, it is set with an empty history
  2. goBack() - go to last entry, notice the entries are kept so that forward() could get us back
  3. forward() - go to next visited entry
  4. visit() - when you enter a new address or click a link, this adds a new entry but truncate the entries which we could forward() to.

Say we start a new tab, this is the empty history.

[ ] 

Then visit url A, B, C in turn.

[ A, B, C]

We are currently at C, we could goBack() to B, then to A

[ A, B, C]

forward() get us to B

[ A, B, C]

Now if we visit a new url D, since we are currently at B, C is truncated.

[ A, B, D]

You are asked to implement a BrowserHistory class to mimic the behavior.

What is time & space complexity of your approach?