Actors and Directors Who Cooperated At Least Three Times - Problem

๐ŸŽฌ Finding Frequent Actor-Director Collaborations

In the entertainment industry, successful partnerships between actors and directors often lead to multiple collaborations. Your task is to analyze a database of movie collaborations and identify the most frequent partnerships.

You are given a table ActorDirector that records every collaboration between actors and directors:

Column NameTypeDescription
actor_idintUnique identifier for the actor
director_idintUnique identifier for the director
timestampintWhen the movie was made (primary key)

Goal: Find all pairs (actor_id, director_id) where the actor has worked with the director on at least 3 movies.

Example: If actor 1 worked with director 1 on movies recorded at timestamps 0, 1, and 2, then this pair should be included in the result because they collaborated 3 times.

Return the result in any order. The focus is on identifying these power partnerships in the film industry!

Input & Output

example_1.sql โ€” Basic Collaboration
$ Input: ActorDirector table: | actor_id | director_id | timestamp | |----------|-------------|----------| | 1 | 1 | 0 | | 1 | 1 | 1 | | 1 | 1 | 2 | | 1 | 2 | 3 | | 1 | 2 | 4 | | 2 | 1 | 5 | | 2 | 1 | 6 |
โ€บ Output: | actor_id | director_id | |----------|-------------| | 1 | 1 |
๐Ÿ’ก Note: Actor 1 worked with Director 1 three times (timestamps 0, 1, 2), which meets our threshold of at least 3 collaborations. All other pairs have fewer than 3 collaborations.
example_2.sql โ€” Multiple Qualifying Pairs
$ Input: ActorDirector table: | actor_id | director_id | timestamp | |----------|-------------|----------| | 1 | 1 | 0 | | 1 | 1 | 1 | | 1 | 1 | 2 | | 2 | 2 | 3 | | 2 | 2 | 4 | | 2 | 2 | 5 | | 2 | 2 | 6 |
โ€บ Output: | actor_id | director_id | |----------|-------------| | 1 | 1 | | 2 | 2 |
๐Ÿ’ก Note: Both pairs qualify: Actor 1 with Director 1 (3 collaborations) and Actor 2 with Director 2 (4 collaborations). Both meet the minimum requirement of 3 collaborations.
example_3.sql โ€” Edge Case: No Qualifying Pairs
$ Input: ActorDirector table: | actor_id | director_id | timestamp | |----------|-------------|----------| | 1 | 1 | 0 | | 1 | 2 | 1 | | 2 | 1 | 2 | | 2 | 3 | 3 | | 3 | 1 | 4 |
โ€บ Output: Empty result set | actor_id | director_id | |----------|-------------|| | | |
๐Ÿ’ก Note: No actor-director pair appears 3 or more times. Each combination appears only once or twice, so no pairs qualify for the result.

Visualization

Tap to expand
๐ŸŽฌ Actor-Director Collaboration Analysis๐Ÿ“Š Raw Movie Data๐ŸŽฌ(1,1,0)๐ŸŽฌ(1,1,1)๐ŸŽฌ(1,1,2)๐ŸŽฌ(1,2,3)๐ŸŽฌ(2,1,4)๐Ÿ”„ GROUP BY MagicGroup (1,1)Count: 3Group (1,2)Count: 1Group (2,1)Count: 1โœจ HAVING COUNT(*) >= 3๐Ÿ† Winner: (1,1)3+ Collaborations๐ŸŽฏ Power Partnership Identified!
Understanding the Visualization
1
Raw Data Collection
Each movie collaboration is recorded with actor, director, and timestamp
2
Group by Partnerships
SQL groups identical actor-director pairs together
3
Count Collaborations
For each group, count total number of movies made together
4
Filter Frequent Pairs
Keep only pairs with 3+ collaborations using HAVING clause
Key Takeaway
๐ŸŽฏ Key Insight: GROUP BY with HAVING is the perfect tool for finding patterns in relational data - it groups, counts, and filters all in one efficient operation!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the data to group and count

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Space for k distinct actor-director pairs in the hash table

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค Number of rows โ‰ค 1000
  • 1 โ‰ค actor_id, director_id โ‰ค 500
  • 0 โ‰ค timestamp โ‰ค 108
  • timestamp is the primary key (each movie is unique)
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
67.5K Views
High Frequency
~8 min Avg. Time
1.9K 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