All problems

Browser History Tabs

Medium · design

A browser has any number of tabs. Each tab has a name and its own history of pages, with one page currently shown. You are given a list of operations. operations[i] is one of the names below, and args[i] is a list of strings holding its arguments:

Every operation produces one result, a string:

A name is given to a tab by only one "open" and never reused, and every "visit", "back", "forward" and "close" names a tab that is currently open. Return the list of results, in order.

Examples

Example 1

Input: operations = ["open", "visit", "visit", "back", "back", "back", "forward", "visit"], args = [["t1", "home"], ["t1", "news"], ["t1", "mail"], ["t1"], ["t1"], ["t1"], ["t1"], ["t1", "maps"]]

Output: ["home", "news", "mail", "news", "home", "home", "news", "maps"]

The third back does nothing because the tab is already at home. The last visit throws away mail, which was ahead of news.

Example 2

Input: operations = ["open", "open", "close", "visit", "reopen", "back", "reopen"], args = [["a", "one"], ["b", "two"], ["b"], ["a", "three"], [], ["b"], []]

Output: ["one", "two", "two", "three", "b", "two", ""]

Tab b comes back with its single page. The second reopen finds nothing left to bring back.

Example 3

Input: operations = ["open", "open", "visit", "close", "close", "reopen", "reopen", "back"], args = [["a", "p"], ["b", "q"], ["b", "r"], ["a"], ["b"], [], [], ["b"]]

Output: ["p", "q", "r", "p", "r", "b", "a", "q"]

Tab b was closed last, so it comes back first, then a. Tab b still has q behind r.

Constraints