Skip to content
BytePatterns

Back and Forward History

EasyLinked Lists#doubly-linked-list#design~20m

Problem

Simulate the history of a single browser tab that starts on a home page. A visit opens a new page after the current one and throws away every page you could previously have gone forward to. A back of k moves up to k pages towards the start, and a forward of k moves up to k pages towards the newest one, stopping early when there is nowhere left to go. Given the home page and a list of operations, return the page you are on after each back or forward, in order.

Examples

Input:  home = "home"
        ops  = [("visit", "a"), ("visit", "b"), ("back", 1),
                ("visit", "c"), ("forward", 1), ("back", 5)]
Output: ['a', 'c', 'home']
Why:    visiting c from a drops b, so forward has nowhere to go
Input:  home = "x", ops = [("back", 2), ("forward", 3)]
Output: ['x', 'x']
Why:    edge case, a fresh tab cannot move either way

Hints

0 / 3

Stuck on the idea rather than the code? Doubly Linked Lists covers it.