0
0
Rest APIprogramming~5 mins

404 Not Found in Rest API - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: 404 Not Found
O(n)
Understanding Time Complexity

When a server responds with a 404 Not Found, it means the requested resource isn't available.

We want to understand how the server's work grows when checking if a resource exists.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

GET /resource/{id}

if resource with id exists in database:
    return 200 OK with resource
else:
    return 404 Not Found

This code checks if a resource exists and returns 404 if it does not.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Searching the database for the resource by id.
  • How many times: Once per request, but the search may scan multiple entries depending on database structure.
How Execution Grows With Input

The time to find the resource depends on how many entries the database has.

Input Size (n)Approx. Operations
10About 10 checks
100About 100 checks
1000About 1000 checks

Pattern observation: The more entries, the longer it takes to find or not find the resource.

Final Time Complexity

Time Complexity: O(n)

This means the time to respond grows linearly with the number of resources to check.

Common Mistake

[X] Wrong: "The server instantly knows if a resource exists without checking anything."

[OK] Correct: The server must look through data to confirm if the resource exists, which takes time depending on data size.

Interview Connect

Understanding how servers find resources helps you explain backend efficiency clearly and confidently.

Self-Check

"What if the database used an index to find resources? How would the time complexity change?"