dsa · medium
Design Browser History
BrowserHistory(homepage) starts on homepage. visit(url) goes to url and drops every page in front of the current one. back(steps) and forward(steps) move **at most** steps pages. If fewer pages exist in that direction, **stop** at the end and still return the current url — never null. (visit is the call that returns null.)
Methods
BrowserHistory(homepage)construct; current page ishomepagevisit(url)go tourland drop any forward history; returnsnullback(steps)move back at moststepspages; if fewer exist, stop at the oldest; return the current url (nevernull)forward(steps)move forward at moststepspages; if fewer exist, stop at the newest; return the current url (nevernull)
Fill in the BrowserHistory class. The starter already walks ops / args and calls your methods — leave the driver at the bottom as-is. Constructors contribute null; booleans print as true / false.
Example
`` BrowserHistory("dojo.shizuha.com") → null visit("pulse.shizuha.com") → null visit("hive.shizuha.com") → null visit("cortex.shizuha.com") → null back(1) → "hive.shizuha.com" back(1) → "pulse.shizuha.com" forward(1) → "hive.shizuha.com" visit("wiki.shizuha.com") → null forward(2) → "wiki.shizuha.com" back(2) → "pulse.shizuha.com" back(7) → "dojo.shizuha.com" ``
Constraints
Calls <= 500
Examples
Example 1
Input: ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["dojo.shizuha.com"],["pulse.shizuha.com"],["hive.shizuha.com"],["cortex.shizuha.com"],[1],[1],[1],["wiki.shizuha.com"],[2],[2],[7]] Expected: [null,null,null,null,"hive.shizuha.com","pulse.shizuha.com","hive.shizuha.com",null,"wiki.shizuha.com","pulse.shizuha.com","dojo.shizuha.com"]