KatInterview

API Design for a Frequency Analysis Service

System DesignModel answer
HardModel answerSystem Designapi-designerror-handlingvalidation

Last updated

Design comprehensive input validation, error handling, and operational safeguards for a RESTful endpoint that returns the most common elements from a dataset.

You're building a RESTful service that accepts a collection of elements and returns the most frequently occurring ones. The algorithmic implementation is out of scope—focus instead on designing a production-ready API surface and its surrounding operational concerns.

Your design should address:

  1. Request/response schema — Define the HTTP method, path, headers, and JSON structure. Provide a concrete example showing both a valid request and its corresponding success response.

  2. Boundary conditions — Specify limits on payload dimensions, element cardinality, result size, element data types, and compression. Justify each choice.

  3. Multi-tier validation — Describe what checks occur at the client library, edge layer, and application service, and in what sequence. Explain how you handle schema evolution.

  4. Identity and access control — Detail authentication mechanisms for external consumers versus internal services, tenant separation, and permission models.

  5. Traffic management — Define rate limiting and quota strategies, including what gets metered and how throttled clients learn when to retry.

  6. Safe retries — Design an idempotency mechanism for POST requests. Handle scenarios where the same identifier arrives with different payloads.

  7. Time budgets and retry guidance — Allocate time across request phases, specify which HTTP status codes signal timeout conditions, and recommend a client-side retry strategy.

  8. Structured error reporting — Create a complete error classification with stable machine-readable codes and a uniform response envelope. Show examples for both client and server errors.

  9. Handling partially invalid input — Decide whether to reject requests containing some malformed elements or process valid ones and report what was skipped.

  10. Load adaptation — Define behavior when facing oversized but syntactically correct requests: return approximate results, apply sampling, or shed load entirely.

  11. Attack surface hardening — Protect against compression exploits, malformed character encodings, excessive JSON depth, ambiguous JSON constructs, and inputs designed to maximize memory or CPU consumption.

  12. Operational visibility — Specify structured logging, metric collection, and distributed tracing requirements. Identify data that must be redacted.

  13. Quality assurance — Outline pre-deployment testing approaches and production alerting criteria.

Make explicit choices about defaults and articulate the engineering trade-offs behind each decision.

Hints

Hint 1

Start by defining strict defaults: fail fast on ambiguous input, return exact results by default, and make every limit explicit and documented.

Hint 2

Think about the order of validation: run cheap checks (authentication, size limits) before expensive ones (parsing, computation) to protect resources.

Hint 3

Design your error codes to be actionable: clients should know whether to retry, fix their request, or escalate to support based solely on the error code.