Practice/Bloomberg/Leetcode 1472. Design Browser History
CodingMust
Design a system to manage video watching history for a streaming platform. Users need the ability to track which videos they've watched and navigate backward and forward through their viewing timeline, similar to how web browsers handle page navigation.
Your task is to implement a TimelineNavigator class that manages this viewing history with the following capabilities:
A critical behavior: when a user watches a new video after navigating backward, all forward history is erased. This mimics real-world navigation where taking a new action eliminates alternative future paths.
TimelineNavigator class with a constructor that accepts an initial video URLwatch(url) which navigates to a new video and removes any forward historyback(steps) which moves backward up to the specified number of steps (limited by available history) and returns the resulting current videoforward(steps) which moves forward up to the specified number of steps (limited by available forward history) and returns the resulting current videocurrent() which returns the URL of the currently selected videoExample 1:
` Operations: navigator = TimelineNavigator("homepage.mp4") navigator.watch("video1.mp4") navigator.watch("video2.mp4") navigator.back(1) navigator.current()
Output: "video1.mp4"
Explanation: Start at homepage.mp4 Watch video1.mp4 (history: [homepage, video1]) Watch video2.mp4 (history: [homepage, video1, video2]) Go back 1 step (now at video1) Current video is video1.mp4 `
Example 2:
` Operations: navigator = TimelineNavigator("start.mp4") navigator.watch("next.mp4") navigator.back(1) navigator.watch("different.mp4") navigator.forward(1) navigator.current()
Output: "different.mp4"
Explanation: Start at start.mp4 Watch next.mp4 (history: [start, next]) Go back 1 step (now at start) Watch different.mp4 (history: [start, different], forward history cleared) Try to go forward 1 step (no forward history exists, stay at different) Current video is different.mp4 `
Example 3:
` Operations: navigator = TimelineNavigator("home.mp4") navigator.watch("a.mp4") navigator.watch("b.mp4") navigator.back(5) navigator.current()
Output: "home.mp4"
Explanation: History: [home, a, b], currently at index 2 Request to go back 5 steps, but only 2 steps available Move back to index 0 (home.mp4) `