0
0
Rest APIprogramming~5 mins

Problem Details (RFC 7807) format in Rest API - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Problem Details (RFC 7807) format
O(n)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

As the number of errors increases, the time to build the response grows proportionally.

Input Size (n)Approx. Operations
1010 additions to the errors object
100100 additions to the errors object
10001000 additions to the errors object

Pattern observation: The work grows directly with the number of errors; doubling errors doubles the work.

Final Time Complexity

Time Complexity: O(n)

This means the time to build the Problem Details response grows linearly with the number of error details included.

Common Mistake

[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.

Interview Connect

Understanding how error response building scales helps you design APIs that handle errors efficiently and predict performance as input grows.

Self-Check

"What if the errors were nested objects instead of simple messages? How would that affect the time complexity?"