Validate Signed API Requests With Strictly Increasing Nonces
Last updated: November 29, 2026
Quick Overview
Write the server-side check for private API calls where the client sends a base64 JSON payload containing a nonce, signed with HMAC-SHA384 using the API secret. Accept a request only if the key exists, the signature matches, and the nonce is within a time window and strictly greater than the last accepted nonce for that key.
Gemini
November 29, 20260
0
0 solved
Write the server-side check for private API calls where the client sends a base64 JSON payload containing a nonce, signed with HMAC-SHA384 using the API secret. Accept a request only if the key exists, the signature matches, and the nonce is within a time window and strictly greater than the last accepted nonce for that key.
Gemini's private REST API authenticates requests with an API key, a signed payload and a nonce that must increase, which blocks replayed order or withdrawal requests. The interviewer tests whether you understand HMAC, constant-time comparison, replay protection and the concurrency trap in the last-nonce update.
What the Interviewer Expects
- Verify the signature before trusting anything inside the payload
- Use constant-time comparison for signatures
- Reject reused or decreasing nonces per key and explain what replay attack this stops
- Add a time window so a captured request cannot be replayed much later against a fresh key state
- Point out the race when two servers check the same key, and fix it with an atomic compare-and-set
- Return distinct error codes without leaking secrets
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How do you handle a client that sends requests in parallel and they arrive out of order?
- Where would you store last-nonce state across a fleet of API gateways?
- How would key rotation and per-key permissions (trading vs withdrawal) fit in?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
Inputs: api key, base64 payload, hex signature, server time in ms. Output: ok or a reason. Rules: unknown key fails; signature must equal HMAC-SHA384(secret, payload); nonce parsed from the payload ...
High-Level Design
Order matters. Check the signature first, because an unsigned payload might be crafted to poison state. Then parse, then time window, then monotonic check, then record. The monotonic rule makes each s...