KatInterview

Debug and Fix a Spring Boot Movie Search Across Five Rules

System DesignModel answer
HardModel answerSystem Designspring-bootdebuggingjpa

Last updated

Repair a broken Spring Boot search endpoint so it handles partial case-insensitive matching, field targeting, popularity ordering, and a published-only filter.

Context

You inherit a Spring Boot backend that exposes a movie search endpoint. The endpoint accepts a search string and a field selector, but it consistently returns wrong results. The logic is spread across three layers — a REST controller, a service class, and a Spring Data JPA repository — and none of the layers has test coverage.

Your job is to diagnose the defects, prove them with tests, and ship a corrected implementation.

The Data Model (given)

A Movie entity has at minimum:

FieldTypeNotes
idLongPrimary key
titleStringThe movie title
starsCollection<String>Cast member names
popularitydouble or intHigher means more popular
publishedbooleanOnly true entries are shown

The Five Rules the Search Must Satisfy

  1. Substring match — a movie matches when the query string appears anywhere inside the searched field, not only when it equals the whole value.
  2. Field targeting — the caller specifies whether to match against title, against stars, or against all fields.
  3. Case-insensitivity — matching must not distinguish upper from lower case in either the query or the stored data.
  4. Popularity ordering — results are sorted from most popular to least popular; if two movies share the same popularity, a secondary key must break the tie deterministically.
  5. Published filter — movies with published = false are never returned, regardless of field mode.

The endpoint's URL, parameter names, and response shape must not change — existing callers depend on them.

Clarifying Questions You Should Raise

Before writing a single line of code, flag any ambiguity that would affect correctness:

  • In all-fields mode, does a movie qualify when the text matches its title or any star, or must it match both?
  • Is the popularity sort descending (most popular first)? What is the explicit tie-breaker after popularity?
  • What should an empty or absent query return: every published movie, an empty list, or an HTTP 400?
  • Is "substring" a plain LIKE '%x%' match, or should it tolerate word prefixes, diacritics, or typos?
  • Are results paginated, and can clients override the sort direction?

Part 1 — Write Failing Tests First

Before touching the application code, write tests that pin down each of the five rules. Each test must fail against the current broken implementation and pass after your fix.

For each test describe:

  • The test data you would seed
  • The input (query string, field mode)
  • The expected output (which movies appear, which do not, what order)
  • Which layer the test exercises (repository, service, or HTTP) and why you chose that layer

Cover at least one interesting interaction between rules — for example, the published filter combined with a star-field search.

Part 2 — Locate and Name the Defects

Explain how you would trace a request from the controller down to the generated SQL, name the specific bug you would expect to find for each of the five rules, and describe how you would lock in the fix so it cannot silently regress.

Part 3 — Write the Corrected Search

Provide the corrected code for field-choice dispatch, partial case-insensitive matching, the published filter, and the sort order. Decide whether each concern is handled in the database query or in Java, and justify that choice.

Hints

Hint 1

Start with the repository layer: print or log the JPQL/SQL that Spring Data actually generates before assuming the bug is in the service or controller. Hibernate's `show_sql` flag or a `@Query` inspection will save a lot of guessing.

Hint 2

Case-insensitive substring matching in JPQL looks like `LOWER(m.title) LIKE LOWER(CONCAT('%', :q, '%'))`. Doing it in Java instead means loading every row from the database first — avoid that unless the catalog is tiny.

Hint 3

The published filter and the field-choice are two independent boolean conditions; make sure the published check is ANDed with the field match, not ORed. One misplaced parenthesis silently bypasses the filter for one field mode.