Browser History Operations
Problem statement
Implement the history of one browser tab, starting at homepage. Process the operations in actions in order. The parallel arrays pages and steps supply each operation’s arguments.
For this exercise, assume the operations have these rules:
visit: visitpages[i]. Discard all forward history, then add the new page after the current entry. Visiting the same identifier again creates a separate history entry.back: move backward by up tosteps[i]entries. Stop at the earliest entry if fewer are available.forward: move forward by up tosteps[i]entries. Stop at the latest entry if fewer are available.
Return the current page after every operation, including visits and operations that cannot move. Treat page identifiers as opaque, case-sensitive strings. An empty operation list returns an empty array.
Function
browserHistory(homepage: String, actions: String[], pages: String[], steps: int[]) → String[]Examples
Example 1
homepage = "home"actions = ["visit","visit","back","visit","forward","back","forward"]pages = ["b","c","","d","","",""]steps = [0,0,1,0,1,10,2]return = ["b","c","b","d","d","home","d"]After returning to b, visiting d removes c from forward history. Large back movement stops at home.
Example 2
homepage = "home"actions = []pages = []steps = []return = []No operations produce no output entries.
Example 3
homepage = "h"actions = ["visit","visit","back","back","forward"]pages = ["a","a","","",""]steps = [0,0,1,1,2]return = ["a","a","a","h","a"]The two visits to a create separate entries. Two one-step back operations are needed to reach h.
Constraints
0 <= actions.length <= 200, andpages.length = steps.length = actions.length.- Each action is exactly
visit,back, orforward. homepageand each visited page contain1to60characters. Allowed characters are English letters, digits, period, slash, colon, underscore and hyphen.- For a visit,
pages[i]is the visited identifier andsteps[i] = 0. - For back or forward,
pages[i]is empty and0 <= steps[i] <= 10^9.