KatInterview

Debug a Failing Django Film Catalog Filter API

System DesignModel answer
HardModel answerSystem Designdjangodebuggingrest-api

Last updated

Fix defects in an existing Django REST API so that a suite of automated tests covering search, filtering, ordering, and error handling all pass.

Background

You have inherited a Django project that exposes a film catalog API. A colleague wrote the models, URL configuration, views, and a test suite before leaving the team. When you run the tests, several of them fail — some with assertion errors on the response body, others with HTTP 500 responses. Your job is to make every test pass by fixing the application code. You are not starting from scratch; the scaffolding is already there.

The endpoints accept an optional free-text search term and a set of filters (genre, director, writer, rating range, release year range) and always require a sort order for the results. Treat the test suite as the authoritative specification. If a test and your intuition disagree, trust the test.

You have 60 minutes.

What You Are Given

  • Django models for films and their related entities (genre, director, writer)
  • URL routing, view functions or class-based views, and serializers
  • A test suite that covers multiple endpoints and parameter combinations
  • The ability to run the full suite (python manage.py test) or individual tests

What You Are Not Asked to Do

  • Build any new models, endpoints, or migrations
  • Modify the tests (assume they are correct and consistent)

Clarifying Questions to Raise Before You Start

Before writing a single fix, a strong candidate asks:

  1. Are the tests the complete specification, or is there also a written description of expected behavior?
  2. When a filter receives an invalid value (e.g., year=abc), should the endpoint return a 400 or silently ignore the filter?
  3. Are text filters (genre, director, writer) exact-match or case-insensitive? Are numeric bounds inclusive or exclusive? Where in the tests would you look to find out?

Part 1 — Triage Before You Touch Anything

You run the full suite and see failures scattered across multiple endpoints. Before editing any code, how do you organize what you see? Walk through:

  • How you group the failures to spot shared root causes
  • The order in which you trace a single failing test from the assertion back to the source
  • Which category of failure you fix first, and why

Part 2 — Filters Return Wrong or Duplicate Results

For each symptom below, describe what you inspect and what defect likely causes it:

  1. Requests that include filters but no search query return zero results or crash, while the same filters combined with a search term work fine.
  2. Filtering by genre, director, or writer returns the wrong films, or returns some films more than once.
  3. Films whose rating or year sits exactly on a boundary value are missing from results that should include them, or appear in results that should exclude them.
  4. One filter appears to be completely ignored — changing its value has no effect on the results.

Part 3 — Ordering and 500 Errors

The result sets are now correct, but some tests still fail because items are in the wrong order, and one endpoint returns HTTP 500 on certain inputs. How do you diagnose and fix each problem?

  • How do you construct a deterministic sort that handles ties the same way the tests expect?
  • How do you read a Django traceback to identify which layer caused the 500?
  • How do you convert bad input into a proper 4xx client error instead of letting it crash?

Hints

Hint 1

Start by grouping test failures by endpoint and error type — assertion errors and 500s have different root causes and different fix strategies. Fix a 500 first because it blocks you from seeing whether the logic underneath is even right.

Hint 2

The most common sources of wrong filter results in Django are: (a) building the queryset inside an `if search_query` branch so filters are never applied without a query, (b) filtering across a ManyToMany or reverse FK without a `.distinct()`, and (c) using `=` where `__iexact` or `__gte`/`__lte` is needed.

Hint 3

For ordering, check whether the tests assert on a secondary sort key when the primary values tie. A single `order_by('rating')` can pass most cases but fail whenever two films share the same rating. Add a stable tie-breaker (e.g., `id`) that matches the expected order in the test fixtures.