Problem 272501 · medium · Phase 02 Linear Data Structures

Browser Tab History

stacks · class design

Design a class TabHistory that models the back and forward buttons of one browser tab.

  • TabHistory(homepage): open the tab on homepage.
  • visit(url): navigate to url from the current page. This discards all forward history.
  • back(steps): go back up to steps pages (stop at the oldest page if fewer are available) and return the current page.
  • forward(steps): go forward up to steps pages (stop at the newest page if fewer are available) and return the current page.

Examples

Input:  ops  = ["TabHistory", "visit", "visit", "visit", "back", "back", "forward", "visit", "forward", "back", "back"]
        args = [["home"], ["news"], ["mail"], ["docs"], [1], [1], [1], ["chat"], [2], [2], [7]]
Output: [None, None, None, None, "mail", "news", "mail", None, "chat", "news", "home"]
Explanation: after visiting "chat" from "mail", "docs" is no longer reachable; forward(2) stays on "chat".

Constraints

  • At most 5000 operations
  • 1 <= steps <= 100
  • Target: O(steps) per call

Goals

  • Keep back and forward pages on two stacks
  • Clear the forward stack when a new page is visited
  • Clamp step counts that exceed the available history
Starting Python…