Code RoomCharge retried without idempotency
HardPrep Room Coding #2217

Charge retried without idempotency

Code reviewNetworking & APIsSenior–Staff~22 min

Review this Java method that retries a payment charge on failure.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 22 min
Mark a line and say what kind of problem it is.0 findings
1PaymentResult charge(String account, long cents) {
2 for (int attempt = 0; attempt < 3; attempt++) {
3 try {
4 HttpResponse<String> r = client.send(
5 HttpRequest.newBuilder(URI.create(PAY_URL))
6 .POST(BodyPublishers.ofString(body(account, cents)))
7 .build(),
8 BodyHandlers.ofString());
9 return parse(r);
10 } catch (IOException e) {
11 continue;
12 }
13 }
14 throw new RuntimeException("charge failed");
15}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.