All practice tasks

Coding (Platform)

Retry Safe Reservations

Fastify · Hard · Idempotency

A project reservation endpoint already handles a single successful request. Under duplicate delivery or concurrent requests, it rejects valid retries or oversells the final seat. A repository write can also fail. Repair the consistency boundary while preserving the HTTP application and public receipt format.

Task

Make reservations atomic and idempotent within one service process, with deterministic concurrency and failure tests.

Contract

  • Request: POST /projects/:id/reservations; :id is the project being reserved. Send the body as JSON with Content-Type: application/json.
  • JSON body: quantity — seats requested; integer from 1 through 10. expectedVersion — project version last read by the caller; positive integer. requestId — identity of this reservation attempt; 1–40 lowercase letters, digits or hyphens. Example: {"quantity": 1, "expectedVersion": 3, "requestId": "reserve-42"}.
  • Authentication: Authorization: Bearer fixture-alpha identifies tenant alpha; fixture-bravo identifies tenant bravo. The supplied authentication helper resolves the tenant. A query/body field is not an identity source.
  • Request delivery and storage: The same request may arrive again after a timeout. Different requests may overlap. Repository operations from different requests may overlap, and a write may fail. Use the provided single-process exclusive boundary. No distributed storage is part of this task.

Acceptance criteria

  • Return HTTP 200 with {id, quantity, remaining, version}.
  • remaining is the seat count after subtracting quantity; version increases by exactly one.
  • Concurrent requests must not reserve more seats than exist.
  • The key is the authenticated tenant plus requestId.
  • Same key and same project, quantity and expected version: return the original receipt without changing stock again.
  • Check for that receipt before checking the project’s current version.
  • Same key with different reservation data: HTTP 409, code IDEMPOTENCY_CONFLICT.
  • Malformed body: HTTP 400, code INVALID_RESERVATION.
  • Missing or unknown bearer token: HTTP 401, code UNAUTHORIZED.
  • Missing, archived or another tenant’s project: HTTP 404, code NOT_FOUND.
  • Version changed: HTTP 409, code VERSION_CONFLICT.
  • Not enough seats: HTTP 409, code INSUFFICIENT_SEATS.
  • Repository write failed: HTTP 503, code RETRY_LATER.
  • Every error body is {code: "THE_CODE"}.
  • Do not consume seats, increment the version or save a successful receipt.
  • Retrying the same request must still be possible.

Constraints

  • At most 10000 projects in the repository; reservation JSON body at most 16 KiB.
  • Use the supplied repository and authenticated tenant resolver. No external database, network service or production credential is needed.
  • The reservation guarantee is single-process only. Do not claim distributed or durable idempotency from an in-memory store.
  • Keep response fields and error codes stable. Do not modify fixtures, manifests or evaluator tests.

Editable files

  • src/app.js — Framework router/controller, middleware and error response integration.
  • src/service.js — Repair filtering/pagination or the reservation consistency boundary.
  • src/helpers.js — Optional helpers; import from service when needed.
  • tests/candidate.test.js — Add the required regression tests here using fixture(t), assert and node:test.

What this task practices

Retry Safe Reservations is a coding (platform) interview exercise that trains prompt interpretation, explicit assumptions, a concrete response, and a clear explanation of tradeoffs. The catalog marks it as hard difficulty. It focuses on Technical Reasoning, Testing, Validation. The signed-in workspace provides the tools for the round and evaluates the attempt against task-specific criteria. Reference solutions, hidden checks, evaluator instructions, and candidate work remain private.

Sign in to startThe workspace and evaluation open after sign in.