InterviewDB Question

Movie Filter: Query and Filter a Movie Catalog by Multiple Criteria with Ranking

Question Details

Problem

You have a movie catalog as a list of movie objects. Implement a filter and ranking engine:

  • filter_movies(genre=None, min_rating=None, max_year=None, director=None) —

return movies matching ALL provided criteria.
- rank_by(field, descending=True) — sort the filtered results by a given field.
- top_n(n) —

return the top N results after filtering and ranking.

python
from dataclasses import dataclass

@dataclass
class Movie:
    title: str
    genre: str
    year: int
    rating: float
    director: str

class MovieFilter:
    def __init__(self, catalog: list[Movie]): ...
    def filter_movies(self, **criteria) -> 'MovieFilter': ...
    def rank_by(self, field: str, descending: bool = True) -> 'MovieFilter': ...
    def top_n(self, n: int) -> list[Movie]: ...
catalog = [Movie("Inception","Sci-Fi",2010,8.8,"Nolan"), ...]
result = (
    MovieFilter(catalog)
    .filter_movies(genre="Sci-Fi", min_rating=7.0)
    .rank_by("rating")
    .top_n(5)
)

Follow-ups

  1. Your filter_movies

returns a new MovieFilter for chaining. How does lazy evaluation help when the catalog has 1 million entries?
2. Add a search(query) method that does fuzzy title matching. What algorithm do you use (Levenshtein, trigram)?
3. How would you support OR conditions (e.g., genre=Sci-Fi OR genre=Action)?
4. Persist the catalog in a SQLite database. Rewrite filter_movies to generate a SQL WHERE clause instead.

Full Details

Problem

You have a movie catalog as a list of movie objects. Implement a filter and ranking engine:

  • filter_movies(genre=None, min_rating=None, max_year=None, director=None) —

return movies matching ALL provided criteria.
- rank_by(field, descending=True) — sort the filtered results by a given field.
- top_n(n) —

return the top N results after filtering and ranking.

python
from dataclasses import dataclass

@dataclass
class Movie:
    title: str
    genre: str
    year: int
    rating: float
    director: str

class MovieFilter:
    def __init__(self, catalog: list[Movie]): ...
    def filter_movies(self, **criteria) -> 'MovieFilter': ...
    def rank_by(self, field: str, descending: bool = True) -> 'MovieFilter': ...
    def top_n(self, n: int) -> list[Movie]: ...
catalog = [Movie("Inception","Sci-Fi",2010,8.8,"Nolan"), ...]
result = (
    MovieFilter(catalog)
    .filter_movies(genre="Sci-Fi", min_rating=7.0)
    .rank_by("rating")
    .top_n(5)
)

Follow-ups

  1. Your filter_movies

returns a new MovieFilter for chaining. How does lazy evaluation help when the catalog has 1 million entries?
2. Add a search(query) method that does fuzzy title matching. What algorithm do you use (Levenshtein, trigram)?
3. How would you support OR conditions (e.g., genre=Sci-Fi OR genre=Action)?
4. Persist the catalog in a SQLite database. Rewrite filter_movies to generate a SQL WHERE clause instead.

About This Question

This is a reported interview question from a sig interview during the phone round.

It covers the following topics: Coding, Sql, Phone .