KatInterview

Repair Async Watch-List Add/Remove Endpoints

System DesignModel answer
MediumModel answerSystem Designdebuggingrest-apihttp-status-codes

Last updated

Debug failing REST handlers that add and remove movies in a watch list, fixing check ordering, ID comparison, persistence timing, and status codes.

Setting

You're handed a Movie DB service where signed-in users own watch lists: they can create, rename, and delete lists, and put movies into them or take them out. The routes and the schema for these operations are already in place, but the unit tests around the two movie-membership endpoints are red. This is a fix-the-bug exercise — read the existing handlers, find what's wrong, correct it. Don't redesign the service or move to a different API style.

The endpoints in question:

  • POST /watchlists/{watchListId}/movies — put a movie into the list. The movie's identifier travels in the request body.
  • DELETE /watchlists/{watchListId}/movies/{movieId} — take a movie out of the list.

Behaviour the tests require

  1. A movie is added to a watch list that exists.
  2. A movie that is already in the list is added again.
  3. A movie is removed from a watch list that exists.
  4. Many movies are added in a row, then removed one at a time, with the list checked after each step.
  5. An add is attempted against a watch list that doesn't exist.
  6. A remove is attempted against a watch list that doesn't exist.

Contract and assumptions

  • 404 Not Found when the watch list — or, for adds, the movie — doesn't exist.
  • 409 Conflict when an add duplicates a movie already in the list.
  • A successful add answers 201 Created or 200 OK; a successful remove answers 200 OK or 204 No Content.
  • The list holds a collection of movie identifiers, and those identifiers may be object/reference types, not plain strings.
  • Persistence is asynchronous: work isn't durable until the save resolves, and handlers must not answer before that.
  • Where the contract leaves a choice of status code, adopt whatever the currently passing tests and neighbouring handlers already do. Don't introduce a new style mid-file.

Worth confirming before you start

  • Are IDs strings or object/reference values, and what counts as "the same" identifier?
  • Which success codes do the surrounding tests expect on add and on remove?
  • Does an add require the movie to exist in the database, or only that the identifier is well-formed?
  • On remove, when the list exists but the movie isn't a member, is that a 404 or a benign no-op?
  • Are in-memory changes persisted automatically, or must every handler save explicitly?
  • Should any of this be idempotent, or must a repeat operation raise an error?

Deliverables

  1. Add path — make scenarios 1, 2, and 5 pass. Settle the order of checks, the status each failure produces, and what a successful add returns after the write lands.
  2. Remove path — make scenarios 3 and 6 pass. Say which checks are shared with add, which are specific to removal, how the deletion becomes durable, and how a missing list differs from a non-member movie.
  3. Sequential consistency — make scenario 4 pass: interleaved adds and removals must leave the list in the right state at every observed point.

Hints

Hint 1

A handler that writes a 404 but keeps going will either answer twice ('headers already sent') or mutate a list it never found. Every failure branch has to return immediately.

Hint 2

If IDs are objects, `===`, `includes`, and `indexOf` compare references and always miss. The classic symptom is `splice(indexOf(id), 1)` returning -1 and quietly deleting the last element instead of the target.

Hint 3

An un-awaited `save()` means the test's next read sees stale data. And on remove, `filter` hands you a new array — if you don't assign it back, nothing actually changed.