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 tosteps, clamped at the two ends:backnever moves before the homepage,forwardnever moves past the newest page. Both return the URL now under the cursor (soback 0/forward 0return 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
6
home
visit a
visit b
back 1
back 5
forward 1
forward 9a
home
a
b7
start
visit p1
visit p2
back 1
visit p3
forward 1
back 2
forward 1p1
p3
start
p1