medium +25 pts

Design Browser History

Implement BrowserHistory with visit, back, and forward operations.

Design a class `BrowserHistory` to represent a simple browser history. The class should support the following operations: - `__init__(self, homepage: str)` — Initializes the browser history with the given homepage. The current page is set to homepage. - `visit(self, url: str) -> None` — Visits `url` from the current page. This clears all forward history (any pages after the current one are removed). - `back(self, steps: int) -> str` — Moves `steps` steps backward in history. If you can only return `x` steps (where `x < steps`), return the earliest page you can reach. After moving, return the current URL. - `forward(self, steps: int) -> str` — Moves `steps` steps forward in history. If you can only return `x` steps (where `x < steps`), return the farthest page you can reach. After moving, return the current URL. Implement the class with exactly these method signatures. You may use any standard list/stack approach. Additionally, you must implement a helper function `run_browser_scenario(operations: list, values: list) -> list` that executes a sequence of operations on a fresh `BrowserHistory` instance and returns a list of results (see Example Code). The first operation is always `"BrowserHistory"` with the homepage as its value; `visit`, `back`, and `forward` follow as described. The function must return a list where `None` is used for `__init__` and `visit`, and strings for `back` and `forward`. The grader will call this `run_browser_scenario` function with the test scenarios. For correctness, your `run_browser_scenario` must handle the exact operation names: `"BrowserHistory"`, `"visit"`, `"back"`, `"forward"`.

Constraints

1 <= steps <= 100 Each URL consists of lowercase letters, digits, dots, and slashes. The length of each URL is between 1 and 100. The number of calls to `visit`, `back`, and `forward` is at most 5000. The homepage is a valid initial URL.

Example

b = BrowserHistory('leetcode.com')
b.visit('google.com')       # None
b.visit('facebook.com')
b.visit('youtube.com')
b.back(1)                   # 'facebook.com'
b.back(1)                   # 'google.com'
b.forward(1)                # 'facebook.com'
b.visit('linkedin.com')     # None
b.forward(2)                # 'linkedin.com'
b.back(2)                   # 'google.com'
b.back(7)                   # 'leetcode.com'

# Alternatively, using the helper:
result = run_browser_scenario(
    ["BrowserHistory", "visit", "visit", "visit", "back", "back", "forward", "visit", "forward", "back", "back"],
    ["leetcode.com", "google.com", "facebook.com", "youtube.com", 1, 1, 1, "linkedin.com", 2, 2, 7]
)
# result == [None, None, None, None, 'facebook.com', 'google.com', 'facebook.com', None, 'linkedin.com', 'google.com', 'leetcode.com']
25 points ~20 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Think of the history as two stacks: one for past pages and one for future pages.
When you visit a new URL, clear the future stack.
Back and forward move a fixed number of steps but stop when the respective stack is empty.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.