KatInterview

Ranking Actors by Film Count in a Unified Object Database

SQL & DataAuto-graded · 19 test cases
MediumAuto-gradedSQL & Datadata-aggregationfilteringdeduplication

Last updated

Tests filtering credits by role, deduplicating person-movie pairs, validating object types in a shared ID space, and applying a deterministic tie-breaking policy.

You have a database where both movies and people share the same identifier space. Each object has a type indicating whether it represents a film or a person. Credits link people to movies through roles, and a single person may appear multiple times for the same movie with different roles—or even duplicate entries for the same role.

Your task is to identify the top actors by counting how many distinct films each person acted in. Only credits with the role 'actor' should count, regardless of case or surrounding whitespace.

You are given two lists representing database tables:

objects: Each entry contains:

  • object_id (int): unique identifier for either a movie or a person
  • object_type (str): either 'movie' or 'person'
  • name (str): the title of the movie or the name of the person

credits: Each entry contains:

  • movie_object_id (int): references an object_id expected to be a movie
  • person_object_id (int): references an object_id expected to be a person
  • role (str or None): the role the person played, such as 'actor' or 'director'

Return the top 10 actors, where each actor is represented as a dictionary with:

  • person_object_id: the person's ID
  • name: the person's name
  • movie_count: the number of distinct movies they acted in

Sort results by movie_count descending, then by person_object_id ascending. If there are fewer than 10 actors, return all of them.

Constraints

  • The role field may have inconsistent casing (e.g., 'Actor', 'actor') or extra whitespace (e.g., ' actor ').
  • The role field may be None or a non-string type.
  • A person can have multiple credit entries for the same movie, including duplicates with the 'actor' role.
  • Credits may reference object_id values that do not exist in the objects list, or may reference objects of the wrong type (e.g., movie_object_id pointing to a person).
  • Only credits where both IDs reference valid objects of the correct type should be counted.
  • Role comparison must be case-insensitive and ignore leading/trailing whitespace.
  • An actor with zero valid acting credits should not appear in the results.

Examples

Example 1

Input: rank_actors_by_film_count([{'object_id': 1, 'object_type': 'movie', 'name': 'First Film'}, {'object_id': 2, 'object_type': 'movie', 'name': 'Second Film'}, {'object_id': 10, 'object_type': 'person', 'name': 'Alice'}, {'object_id': 20, 'object_type': 'person', 'name': 'Bob'}], [{'movie_object_id': 1, 'person_object_id': 10, 'role': 'actor'}, {'movie_object_id': 1, 'person_object_id': 10, 'role': 'director'}, {'movie_object_id': 1, 'person_object_id': 10, 'role': 'actor'}, {'movie_object_id': 2, 'person_object_id': 10, 'role': 'actor'}, {'movie_object_id': 1, 'person_object_id': 20, 'role': 'actor'}])

Output: [{'person_object_id': 10, 'name': 'Alice', 'movie_count': 2}, {'person_object_id': 20, 'name': 'Bob', 'movie_count': 1}]

Example 2

Input: rank_actors_by_film_count([{'object_id': 1, 'object_type': 'movie', 'name': 'A'}, {'object_id': 2, 'object_type': 'movie', 'name': 'B'}, {'object_id': 3, 'object_type': 'movie', 'name': 'C'}, {'object_id': 11, 'object_type': 'person', 'name': 'P1'}, {'object_id': 12, 'object_type': 'person', 'name': 'P2'}, {'object_id': 13, 'object_type': 'person', 'name': 'P3'}], [{'movie_object_id': 1, 'person_object_id': 11, 'role': 'Actor'}, {'movie_object_id': 2, 'person_object_id': 11, 'role': ' actor '}, {'movie_object_id': 1, 'person_object_id': 12, 'role': 'actor'}, {'movie_object_id': 3, 'person_object_id': 12, 'role': 'actor'}, {'movie_object_id': 1, 'person_object_id': 13, 'role': 'director'}, {'movie_object_id': 2, 'person_object_id': 13, 'role': None}])

Output: [{'person_object_id': 11, 'name': 'P1', 'movie_count': 2}, {'person_object_id': 12, 'name': 'P2', 'movie_count': 2}]

Example 3

Input: rank_actors_by_film_count([{'object_id': 7, 'object_type': 'movie', 'name': 'Only Movie'}, {'object_id': 9, 'object_type': 'person', 'name': 'Only Actor'}], [{'movie_object_id': 7, 'person_object_id': 9, 'role': 'actor'}, {'movie_object_id': 7, 'person_object_id': 9, 'role': 'actor'}, {'movie_object_id': 999, 'person_object_id': 9, 'role': 'actor'}, {'movie_object_id': 7, 'person_object_id': 7, 'role': 'actor'}])

Output: [{'person_object_id': 9, 'name': 'Only Actor', 'movie_count': 1}]

Hints

Hint 1

Consider how multiple credit rows for the same person and movie should be handled to avoid inflating the count.

Hint 2

Validate that movie_object_id actually points to a movie and person_object_id actually points to a person before counting.

Hint 3

Normalize the role string by stripping whitespace and converting to lowercase before comparing to 'actor'.