Problem Details (RFC 7807) format in Rest API - Time & Space Complexity
When using the Problem Details format in REST APIs, it's important to understand how the time to create and send error responses grows as the number of error details increases.
We want to know how the processing time changes when more problem details are included.
Analyze the time complexity of the following code snippet.
// Pseudocode for building a Problem Details response
function createProblemDetails(errors) {
let problem = {
type: "https://example.com/probs/out-of-credit",
title: "You do not have enough credit.",
status: 403,
detail: "Your current balance is 30, but that costs 50.",
instance: "/account/12345/transactions/abc",
errors: {}
};
for (let err of errors) {
problem.errors[err.field] = err.message;
}
return problem;
}
This code builds a Problem Details JSON object including multiple error messages keyed by field names.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over the list of error objects to add each error to the response.
- How many times: Once for each error in the input list.
As the number of errors increases, the time to build the response grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions to the errors object |
| 100 | 100 additions to the errors object |
| 1000 | 1000 additions to the errors object |
Pattern observation: The work grows directly with the number of errors; doubling errors doubles the work.
Time Complexity: O(n)
This means the time to build the Problem Details response grows linearly with the number of error details included.
[X] Wrong: "Adding more error details won't affect response time much because it's just a small object."
[OK] Correct: Each error detail requires an operation to add it to the response, so more errors mean more work and longer processing time.
Understanding how error response building scales helps you design APIs that handle errors efficiently and predict performance as input grows.
"What if the errors were nested objects instead of simple messages? How would that affect the time complexity?"