Practice/Amazon/Design a System to Track Recently Viewed Items
Object Oriented DesignMust
Design and implement a class that tracks recently viewed items for an e-commerce platform. The tracker should maintain a fixed-size history of item views, automatically removing the oldest items when the capacity is exceeded. When a user views an item that's already in their history, it should be moved to the most recent position rather than creating a duplicate entry.
Your RecentItemsTracker class should support the following operations:
get_recent_items() method should return items ordered from most recent to least recentExample 1: Basic Usage
tracker = RecentItemsTracker(3) tracker.view_item(101) tracker.view_item(102) tracker.view_item(103) tracker.get_recent_items() // Returns: [103, 102, 101]
Example 2: Exceeding Capacity
tracker = RecentItemsTracker(2) tracker.view_item(1) tracker.view_item(2) tracker.view_item(3) // Item 1 is evicted tracker.get_recent_items() // Returns: [3, 2]
Example 3: Updating Position
tracker = RecentItemsTracker(3) tracker.view_item(10) tracker.view_item(20) tracker.view_item(30) tracker.view_item(20) // Move 20 to front tracker.get_recent_items() // Returns: [20, 30, 10]