Domain 1EasyCORE

Friendly Movies Streamed Last Month

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`.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#**FAANG asked:** 1

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:

  • Streams table
  • Users table

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 Streams table (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

  1. Join Tables: First, join the Streams table with the Users table on user_id to ensure we have valid user information.
  2. Filter by Date: Filter the joined records to include only those streams that occurred last month.
  3. Group and Count: Group the filtered records by movie_id and count the distinct user_ids for each movie.
  4. 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 nunique ensures that duplicate streams (same user watching the same movie on the same date) are counted only once.