DELETE for removing resources in Rest API - Time & Space Complexity
When we use DELETE requests in REST APIs, we want to know how long it takes to remove data as the amount of data grows.
We ask: How does the time to delete change when there are more resources?
Analyze the time complexity of the following code snippet.
DELETE /items/{id}
// Server receives DELETE request
// Finds item by id
// Removes item from database
// Sends response confirming deletion
This code deletes one item identified by its unique id from the database.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Searching for the item by its id in the database.
- How many times: This happens once per DELETE request.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to find the item |
| 100 | About 100 steps to find the item |
| 1000 | About 1000 steps to find the item |
Pattern observation: The time to find and delete grows roughly in direct proportion to the number of items.
Time Complexity: O(n)
This means the time to delete an item grows linearly with the number of items in the database.
[X] Wrong: "Deleting one item always takes the same time, no matter how many items exist."
[OK] Correct: If the database searches items by scanning all entries, more items mean more time to find the one to delete.
Understanding how DELETE requests scale helps you explain backend performance clearly and shows you know how data size affects response time.
"What if the database used an index to find items by id? How would the time complexity change?"