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:

  1. 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.
  2. 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:

  • Movies table: Contains movie_id and title
  • Users table: Contains user_id and name
  • MovieRating table: 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
Movie Rating Analytics WorkflowUser AnalysisGROUP BY user_idCOUNT(*) ratingsORDER BY count DESCDate FilteringWHERE created_atBETWEEN 2020-02-01AND 2020-02-29Movie AnalysisGROUP BY movie_idAVG(rating)ORDER BY avg DESCTie BreakingORDER BY name ASCORDER BY title ASCLexicographicalDatabase Optimizationโ€ข Index on (user_id, created_at)โ€ข Index on (movie_id, created_at)โ€ข Hash aggregation for GROUP BYโ€ข Query plan optimizationUNION ALL ResultsMost Active User Name+Top Feb 2020 MovieTime Complexity: O(R log R) where R = number of ratingsSpace Complexity: O(U + M) where U = users, M = movies
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

n
2n
โšก Linearithmic
Space Complexity
O(U + M)

Database manages memory efficiently, only storing unique users and movies temporarily

n
2n
โœ“ 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
Asked in
Netflix 45 Amazon 38 Google 32 Meta 28 Microsoft 25
38.2K Views
High Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen