1495. Friendly Movies Streamed Last Month
Model: qwen2.5-coder:14b
Topics:
FAANG asked: 1
Acceptance rate: 51.3%
Problem Breakdown
The problem asks us to find the friendly movies streamed last month. A movie is considered friendly if it was watched by at least 10 different users. We are given two tables: Streams and Users. The Streams table contains records of user streams with columns user_id, movie_id, and stream_date. The Users table contains user information with columns user_id and username.
Inputs:
StreamstableUserstable
Outputs:
- A list of movie IDs that are friendly.
Implicit Constraints/Tricky Edge Cases:
- If there are no streams in the last month, the output should be an empty list.
- If a movie is watched by fewer than 10 users, it should not be included in the result.
- Duplicate entries in the
Streamstable (same user watching the same movie on the same date) should be ignored.
Intuition
The brute-force approach would involve iterating through each stream record and counting unique users for each movie. This would require multiple passes over the data, leading to a high time complexity of O(n^2), where n is the number of streams.
A better approach involves using SQL aggregation functions to count distinct users per movie efficiently. By filtering out movies with fewer than 10 unique viewers, we can achieve this in a single pass through the data, resulting in a more optimal solution.
Approach
- Join Tables: First, join the
Streamstable with theUserstable onuser_idto ensure we have valid user information. - Filter by Date: Filter the joined records to include only those streams that occurred last month.
- Group and Count: Group the filtered records by
movie_idand count the distinctuser_ids for each movie. - Filter Friendly Movies: Select movies where the count of unique users is at least 10.
Complexity Analysis
- Time Complexity: O(n), where n is the number of stream records in the last month. The join operation and aggregation are linear with respect to the input size.
- Space Complexity: O(m), where m is the number of distinct movies streamed last month. This accounts for the space used by the grouped results.
Solution
import pandas as pd
def friendly_movies_last_month(streams: pd.DataFrame, users: pd.DataFrame) -> pd.DataFrame:
# Join streams with users to ensure valid user information
merged_df = pd.merge(streams, users, on='user_id')
# Filter streams from the last month (assuming current date is known)
last_month_start = '2023-10-01' # Example start of last month
last_month_end = '2023-10-31' # Example end of last month
filtered_df = merged_df[(merged_df['stream_date'] >= last_month_start) & (merged_df['stream_date'] <= last_month_end)]
# Group by movie_id and count distinct users
grouped_df = filtered_df.groupby('movie_id').agg({'user_id': 'nunique'}).reset_index()
# Filter movies with at least 10 unique viewers
friendly_movies = grouped_df[grouped_df['user_id'] >= 10]['movie_id']
return pd.DataFrame(friendly_movies, columns=['movie_id'])
Edge Cases Handled
- No Streams Last Month: If there are no streams in the last month, the filtered DataFrame will be empty, and the final result will also be an empty list.
- Fewer Than 10 Unique Viewers: Movies with fewer than 10 unique viewers are excluded by the filter condition
user_id >= 10. - Duplicate Streams: The use of
nuniqueensures that duplicate streams (same user watching the same movie on the same date) are counted only once.