404 Not Found in Rest API - Time & Space 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.
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 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.
The time to find the resource depends on how many entries the database has.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The more entries, the longer it takes to find or not find the resource.
Time Complexity: O(n)
This means the time to respond grows linearly with the number of resources to check.
[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.
Understanding how servers find resources helps you explain backend efficiency clearly and confidently.
"What if the database used an index to find resources? How would the time complexity change?"