0
0
Rest APIprogramming~5 mins

Partial success handling in Rest API - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Partial success handling
O(n)
Understanding Time Complexity

When handling partial success in REST APIs, some operations succeed while others fail. We want to understand how the time to process these results grows as the number of operations increases.

How does the processing time change when more items are involved?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


// Assume responses is a list of results from multiple API calls
for (let i = 0; i < responses.length; i++) {
  if (responses[i].status === 'success') {
    processSuccess(responses[i].data);
  } else {
    processFailure(responses[i].error);
  }
}
    

This code loops through each response and processes it based on success or failure.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Looping through each response item once.
  • How many times: Exactly once per response, so as many times as the number of responses.
How Execution Grows With Input

As the number of responses grows, the code processes each one individually.

Input Size (n)Approx. Operations
1010 processing steps
100100 processing steps
10001000 processing steps

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

Final Time Complexity

Time Complexity: O(n)

This means the time to handle partial success grows linearly with the number of responses.

Common Mistake

[X] Wrong: "Partial success handling is faster because it skips failed items."

[OK] Correct: Even failed items must be checked and processed, so the code still touches every item once.

Interview Connect

Understanding how partial success handling scales helps you explain API response processing clearly and confidently in interviews.

Self-Check

"What if processSuccess or processFailure themselves loop over data inside each response? How would that affect the time complexity?"