SDE Path

Browser History

Easy

Browser History

Model a browser's back/forward navigation: a single cursor over the visited pages, where opening a new page throws away the forward history.

Requirements

  • The browser starts on homepage, which is the current page (no forward or backward history yet).
  • visit(url) appends a new page after the current one, makes it current, and discards any forward history — the pages you had gone back from are gone.
  • back(steps) / forward(steps) move the cursor by up to steps, clamped at the two ends: back never moves before the homepage, forward never moves past the newest page. Both return the URL now under the cursor (so back 0 / forward 0 return the current URL unchanged).
  • URLs are whitespace-free tokens.

API to implement

Implement the class BrowserHistory. The I/O driver is already written in the starter code — it parses the command script below and calls your methods. You only implement the class.

| Command | Returns | Behavior | |---|---|---| | visit <url> | — | Navigate to url from the current page. This becomes the new current page and clears all forward history. | | back <steps> | string | Move back up to steps pages, clamped at the homepage (never past the start). Return the URL you land on. | | forward <steps> | string | Move forward up to steps pages, clamped at the most recent page. Return the URL you land on. |

Input format

The first line contains Q, the number of operations. The second line contains the constructor argument: homepage — the URL the browser starts on (the initial current page). Each of the next Q lines is one command as listed above. String arguments contain no spaces.

Output format

For each command with a non-void return, print its result on its own line, in order. The driver does this for you — your methods just return values.

This is a design problem: the hidden tests exercise edge cases and interaction sequences, not performance tricks. Model the state cleanly and the tests will pass.

Sample cases

Example 1
Input
6
home
visit a
visit b
back 1
back 5
forward 1
forward 9
Expected output
a
home
a
b
Example 2
Input
7
start
visit p1
visit p2
back 1
visit p3
forward 1
back 2
forward 1
Expected output
p1
p3
start
p1