Early Access: 87 spots left.

Claim
AI Coding InterviewAI Coding InterviewOrder Report API Bug Hunt: Six Integration Bugs

Problems

Order Report API Bug Hunt: Six Integration Bugs

Hard·Tagsai-coding-interviewbug-huntrest-apijsonretriespagination

Problem Statement

This is a bug hunt against a REST API. `ReportService` builds a customer report by calling three backend endpoints, parsing their JSON responses, and applying a few business rules. It compiles and runs, but it has six production bugs: some in how it reads JSON, some in how it handles HTTP responses, and some in the business logic. Six test scenarios fail until each bug is fixed. Find and fix all six. This is what integration code looks like at work. The backend is not yours, its responses are not always tidy, and it sometimes fails. Most bugs in this kind of code are not in the algorithm. They come from trusting a field to always be there, trusting a status code to always be 200, or trusting a number to survive a conversion. How the backend works in this problem `ReportService` talks to the backend only through `HttpClient.call(method, path)`, which returns an `HttpResponse` with a `status` and a raw JSON `body` string. In the tests, `http` is a fake server that answers from fixed data, so there is no real network. It also counts how many times each request is made, which is how retries are checked. The API `GET /users/{userId}` ```json {"id": 7, "name": "Meera Iyer", "email": "user7@example.com", "address": {"street": "12 Lake Rd", "city": "Pune"}} ``` - `address` can be an object, `null`, or missing entirely. When it is an object, `city` can be missing. - An unknown user returns `404` with `{"error": "user not found"}`. `GET /orders?userId={userId}` for the first page, then `GET /orders?userId={userId}&cursor={nextCursor}` for each next page ```json {"orders": [ {"id": "o-701", "status": "COMPLETED", "placedAt": "2026-10-01T10:00:00Z", "items": [{"productId": "p-mug", "quantity": 1}]} ], "nextCursor": "c-2"} ``` - `status` is one of `COMPLETED`, `PENDING`, `CANCELLED`, `REFUNDED`. - `nextCursor` is `null` on the last page. `GET /products/{productId}` ```json {"id": "p-mug", "name": "Ceramic Mug", "price": "19.99", "currency": "USD"} ``` - `price` is a string of dollars with two decimal places. Any endpoint can briefly return a `5xx` status (`500`, `502`, `503`) with an `{"error": ...}` body. The code you are given ```java interface HttpClient { HttpResponse call(String method, String path); } class HttpResponse { final int status; final String body; } class CustomerReport { final long userId; final String name; final String city; final int completedOrders; final long totalSpentCents; } class ReportService { ReportService(HttpClient http); CustomerReport buildReport(long userId); } class ApiException extends RuntimeException { final int status; } class NotFoundException extends RuntimeException { } ``` `Json`, `JsonObject` and `JsonArray` are a small JSON library included in the file. They work correctly, so treat them like a library you would not edit. `Json.parseObject(text)` parses a body. `getString`, `getLong`, `getObject` and `getArray` throw `JsonException` when the key is missing, `null`, or the wrong type. `isNull(key)` is true when the key is missing or `null`. `optString(key, fallback)` returns the fallback instead of throwing. What `buildReport` must return - `name` comes from the user. - `city` is the user's `address.city`, or `"Unknown"` when the address is `null`, missing, or has no city. - `completedOrders` counts only orders whose status is `COMPLETED`. - `totalSpentCents` is the sum of `quantity x price` over the items of `COMPLETED` orders, in exact cents. - Orders from every page are included. How HTTP responses must be handled - `200`: parse the body. - `5xx`: retry the same request, with at most 3 attempts in total. If the last attempt is still `5xx`, throw `ApiException` with that status. - `404`: throw `NotFoundException` right away, with no retry. - Any other status: throw `ApiException` with that status. The six test scenarios 1. price_parsed_to_exact_cents: totals are exact to the cent for prices like `"19.99"`, `"5.10"` and `"0.29"`. 2. missing_address_defaults_to_unknown: a user whose address is `null`, missing, or has no city gets city `"Unknown"`, and the rest of the report is still built. 3. only_completed_orders_count: `PENDING`, `CANCELLED` and `REFUNDED` orders are left out of both the count and the total. 4. all_order_pages_included: a user whose orders span three pages gets all of them. 5. unknown_user_throws_not_found (hidden): a `404` user throws `NotFoundException` after exactly one call. 6. transient_errors_retried (hidden): a request that fails with `503` or `502` and then succeeds produces a normal report, and a service that keeps returning `503` fails with `ApiException` after exactly 3 attempts. The scenarios are independent, so you can fix them in any order and watch the pass count go up one at a time.

Examples

Example 1
Input
GET /products/p-mug returns price "19.99"; user 7 bought 1 mug
Output
"totalSpentCents = 1999"
Why
The total must be exact. Being off by one cent is a failure, the same as it would be on an invoice.
Example 2
Input
GET /users/21 returns {"id": 21, "name": "Arjun Mehta", "address": null, ...}
Output
"city = \"Unknown\"; the report is still built"
Why
A null or missing address is valid data from this API, not an error.
Example 3
Input
GET /orders?userId=51 returns 503, then 503, then 200
Output
"a normal report; the orders endpoint was called exactly 3 times"
Why
A 5xx is retried. The third attempt succeeds, so the caller never sees the outage.

Constraints

  • •buildReport talks to the backend only through HttpClient.call(method, path). Paths must match the API exactly, for example /orders?userId=41&cursor=c-2.
  • •totalSpentCents is exact integer cents. Prices are dollar strings with two decimal places.
  • •city is "Unknown" when the address is null, missing, or has no city.
  • •Only COMPLETED orders count toward completedOrders and totalSpentCents.
  • •Every page of orders is fetched until nextCursor is null.
  • •5xx responses are retried, with at most 3 attempts in total, then ApiException. 404 throws NotFoundException with no retry. Any other non-200 status throws ApiException.

Hints

Stuck? Reveal a nudge toward the right pattern, one step at a time.

Hint 1
Start by running the tests and reading every failure message before you open the code. Each message names the user id, the field, the expected value and the value you returned. Each scenario exercises one behavior, so each failure points at one part of ReportService.
Hint 2
Totals a few cents short: a double cannot store 19.99 exactly. 19.99 * 100 comes out as 1998.9999999999998, and casting to long drops the fraction. Convert the price string without going through double, for example new BigDecimal(price).movePointRight(2).longValueExact(). Math.round also works for two-decimal prices.
Hint 3
A JsonException about 'address': getObject throws when a key is null or missing, and the API allows both. Check isNull("address") before reading it, then isNull("city") inside it. isNull is true for a null key and for a missing key, so one check covers both cases.
Hint 4
Too many orders counted: the code skips the one status it knows is bad. The rule is the opposite: count only COMPLETED. Listing what to include is safer than listing what to exclude, because a new status (REFUNDED, PENDING) falls into the right bucket automatically.
Hint 5
Only the first page of orders arrives: compare the key the code reads for the next cursor with the field name in the API response. optString returns its fallback when a key is missing, so a wrong key name fails quietly instead of throwing.
Hint 6
A 404 that turns into a JsonException: look at which status codes getJson treats as errors. A 404 body is {"error": ...}, not a user, so parsing it is already wrong. Throw NotFoundException for 404, and ApiException for any other status that is not 200.
Hint 7
Retries that never happen: the retry loop reacts to one exact status code, but servers also send 502 and 503. Retry any status of 500 or above, keep the 3-attempt limit, and make sure a 404 is never retried.
Hint 8
Using the assistant: it sees your code and your last run, so ask about one failure at a time. 'user 7's total is 11 cents short, which line could lose cents?' gets a precise answer. 'Fix all the bugs' tends to get a rewrite of the whole service that is hard to check.