Listing orders by date range
API / backend · Difficulty 2/5 · TypeScript · 35 linesOrigin: Fictional
Task
What the AI was asked to do
Write the filter for the GET /orders?from=YYYY-MM-DD&to=YYYY-MM-DD endpoint used by the reports screen. Both days are part of the range, and days are in UTC. If a date is missing or invalid, or from is after to, answer 400 with an error code; otherwise answer 200 with the orders in the range.
Handoff note
What the AI wrote when it handed over the code:
“Dates are checked for format and validity (days such as February 31 are rejected), and a reversed range gets a 400. The comparison runs in UTC, so there are no time zone issues.”
The code the AI wrote
TypeScript35 linesMark the lines you think are wrong; give each mark a severity and a one-sentence reason.
Click or tap a line. With a keyboard, move to the line and press Enter.
How to pick a severity
- CriticalData loss, a security hole or lost money
- MajorThe user gets a wrong result in a case the task asks for
- MinorA rare edge case outside the task, or readability
In the Testing topic, severity is set by the effect of the broken implementation the test doesn't catch.
Your reason isn't graded; you'll compare it with a sample answer.
export interface Order { id: string; totalCents: number; /** UTC, ISO 8601: "2026-10-10T15:00:00Z" */ createdAt: string;}const DAY_PATTERN = /^\d{4}-\d{2}-\d{2}$/;function parseDay(value: string | undefined): Date | null { if (value === undefined || !DAY_PATTERN.test(value)) { return null; } const day = new Date(`${value}T00:00:00Z`); if (Number.isNaN(day.getTime()) || !day.toISOString().startsWith(value)) { return null; } return day;}export function ordersInRange( orders: Order[], query: { from?: string; to?: string },) { const from = parseDay(query.from); const to = parseDay(query.to); if (from === null || to === null || from > to) { return { status: 400, body: { error: "invalid_range" } }; } const matching = orders.filter((order) => { const createdAt = new Date(order.createdAt); return createdAt >= from && createdAt < to; }); return { status: 200, body: { orders: matching } };}The test that proves it
The right option is the test that fails on this code, which means it exposes the bug.
Submitting sends seven pieces of information to a counter; your reason text stays on your device. Data