Movie Rating - Problem
You're building an analytics dashboard for a movie review platform! You have three interconnected database tables containing information about movies, users, and their ratings.
Your task is to solve two distinct problems:
- Find the most active reviewer: Identify the user who has rated the greatest number of movies. If there's a tie, return the lexicographically smaller username.
- Find February 2020's top movie: Identify the movie with the highest average rating specifically in February 2020. If there's a tie, return the lexicographically smaller movie title.
The database schema consists of:
Moviestable: Contains movie_id and titleUserstable: Contains user_id and nameMovieRatingtable: Contains movie_id, user_id, rating, and created_at
Return format: Your result should contain exactly two rows - the first row with the most active user's name, and the second row with the top-rated movie title from February 2020.
Input & Output
example_1.sql โ Basic Case
$
Input:
Movies: [(1,'Avatar'),(2,'Endgame'),(3,'Titanic')]
Users: [(1,'Daniel'),(2,'Monica'),(3,'Maria')]
MovieRating: [(1,1,3,'2020-02-05'),(1,2,4,'2020-02-11'),(1,3,2,'2020-02-17'),(2,1,5,'2020-02-20'),(2,2,3,'2020-02-15'),(2,3,4,'2020-02-10')]
โบ
Output:
Daniel
Endgame
๐ก Note:
Daniel rated 3 movies (most active). In February 2020, Endgame has average rating 3.5, Avatar has 4.0, Titanic has 3.0. Avatar has the highest average rating.
example_2.sql โ Tie Breaking
$
Input:
Movies: [(1,'Avengers'),(2,'Batman')]
Users: [(1,'Alice'),(2,'Bob')]
MovieRating: [(1,1,4,'2020-02-10'),(1,2,4,'2020-02-15'),(2,1,4,'2020-02-20'),(2,2,4,'2020-02-25')]
โบ
Output:
Alice
Avengers
๐ก Note:
Both Alice and Bob rated 2 movies each (tie), but Alice comes first lexicographically. Both movies have same average rating (4.0), but Avengers comes first lexicographically.
example_3.sql โ Date Filtering Edge Case
$
Input:
Movies: [(1,'Movie A'),(2,'Movie B')]
Users: [(1,'User1'),(2,'User2')]
MovieRating: [(1,1,5,'2020-01-31'),(1,2,3,'2020-02-01'),(2,1,4,'2020-02-29'),(2,2,2,'2020-03-01')]
โบ
Output:
User1
Movie A
๐ก Note:
User1 rated 2 movies vs User2's 2 movies, but User1 is lexicographically smaller. Only ratings from 2020-02-01 to 2020-02-29 count for February, so Movie A gets rating 3, Movie B gets rating 4. Movie B wins with higher average.
Visualization
Tap to expand
Understanding the Visualization
1
User Activity Analysis
Count how many movies each user has rated, then find the most active reviewer
2
Time-based Filtering
Filter ratings specifically for February 2020 using date conditions
3
Movie Rating Aggregation
Calculate average ratings for each movie within the time period
4
Tie-breaking Logic
Handle ties using lexicographical ordering (alphabetical sorting)
5
Result Combination
Combine both analytics results into a single response
Key Takeaway
๐ฏ Key Insight: Leverage SQL's built-in GROUP BY aggregation with proper indexing and ORDER BY clauses for both optimal performance and automatic tie-breaking. The database engine handles complex optimizations better than application-level processing.
Time & Space Complexity
Time Complexity
O(R log R)
R ratings with efficient GROUP BY operations and sorting, leveraging database indexes
โก Linearithmic
Space Complexity
O(U + M)
Database manages memory efficiently, only storing unique users and movies temporarily
โ Linear Space
Constraints
- 1 โค Number of movies, users โค 1000
- 1 โค Number of ratings โค 104
- 1 โค rating โค 5
- created_at is a valid date
- Each (movie_id, user_id) pair appears at most once in MovieRating table
- All user names and movie titles are unique
- February 2020 has at least one rating
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code