Search⌘ K

Feature #7: Browse Ratings

Understand how to implement a browse ratings feature for movies using a stack data structure. Explore how to maintain viewing history and retrieve the highest-rated title efficiently with constant-time operations. Gain insight into designing a max stack supporting push, pop, and get max operations, enhancing user experience by enabling quick back navigation and rating lookup.

Description

In this feature, the user will be able to randomly browse through movie titles and read their summaries and reviews. We want to enable a Back button so the user can return to the previous title in the viewing history. We also want the user to immediately get the title with the highest viewer rating from their viewing history. We want to implement both of these operations in constant time to provide a good user experience.

We’ll be provided with a sequential input of ratings to simulate the user viewing them one by one. For simplicity, we’ll assume that the movie ratings are all unique.

Let’s say that a user has browsed through the movies with the following ratings:

Solution

To enable a Back button, we need to store the user’s viewing history in some data structure. Pressing the Back button fetches the last viewed item. This indicates a last in first out (LIFO) structure, which is characteristic of a stack. In a stack, push and pop operations can be easily implemented in O(1)O(1). However, the stack doesn’t allow random ...