Design Task: API, Validation, and Error Handling for a Top-K Frequency Service
Context
Design a production-grade service that, given an array of elements, returns the top-k most frequent values. Focus specifically on API design, input validation, and error handling, with clear operational guardrails.
Requirements
-
Define the API surface:
-
Request/response schema, including versioning and content types.
-
Limits: payload size, bounds on k, element type domains and sizes.
-
Idempotency semantics and request deduplication.
-
Authentication and authorization.
-
Rate limits and quotas.
-
Timeouts and service-level objectives.
-
Error taxonomy: classify client vs. server errors and include structured error responses.
-
Describe validation layers and schema enforcement:
-
Client-side validation expectations.
-
Gateway/WAF validation and protections.
-
Service-level validation and enforcement.
-
How partial-failure behavior should work (e.g., invalid elements in the array).
-
Observability and safety:
-
Metrics, logs, and distributed traces.
-
Safeguards for malformed or adversarial payloads (e.g., large, deeply nested, high-cardinality inputs).
-
Testing and production readiness:
-
How you would test the system pre-deployment (unit, integration, fuzz, property-based).
-
How you would monitor, alert, and run this safely in production.
Assume synchronous processing for modest requests, with an option to propose an asynchronous path for larger payloads if needed.