Batch delete patterns in Rest API - Time & Space Complexity
When deleting many items at once using a batch delete in a REST API, it is important to understand how the time taken grows as the number of items increases.
We want to know how the work done changes when we delete more items in one request.
Analyze the time complexity of the following code snippet.
// Example batch delete endpoint
app.delete('/items', async (req, res) => {
const ids = req.body.ids; // array of item IDs to delete
for (const id of ids) {
await database.deleteItemById(id);
}
res.send({ message: 'Items deleted' });
});
This code deletes each item one by one by looping through the list of IDs and deleting each from the database.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the list of IDs and deleting each item individually.
- How many times: Once for each item in the input list.
As the number of items to delete grows, the total work grows roughly the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 delete calls |
| 100 | 100 delete calls |
| 1000 | 1000 delete calls |
Pattern observation: The work grows linearly with the number of items to delete.
Time Complexity: O(n)
This means the time to delete items grows directly in proportion to how many items you want to delete.
[X] Wrong: "Deleting multiple items in one batch always takes the same time as deleting one item."
[OK] Correct: Each item still needs to be deleted, so the total time adds up with more items.
Understanding how batch operations scale helps you design efficient APIs and explain your choices clearly in interviews.
"What if the batch delete used a single database query to delete all items at once? How would the time complexity change?"
Practice
What is the main advantage of using batch delete in a REST API?
Solution
Step 1: Understand batch delete purpose
Batch delete is designed to remove many items at once, not one by one.Step 2: Identify the benefit
Removing multiple items in one request saves time and reduces server load.Final Answer:
It removes multiple items with a single request, saving time. -> Option AQuick Check:
Batch delete = single request for many items [OK]
- Thinking batch delete backs up data
- Confusing batch delete with encryption
- Assuming batch delete needs multiple requests
Which of the following is the correct HTTP method to perform a batch delete in a REST API?
Solution
Step 1: Recall HTTP methods for REST
DELETE method is used to remove resources in REST APIs.Step 2: Match method to batch delete
Batch delete also uses DELETE but with multiple IDs in body or query.Final Answer:
DELETE -> Option CQuick Check:
Batch delete uses DELETE method [OK]
- Using POST instead of DELETE for deletion
- Confusing GET with delete operations
- Using PUT which is for updates
Given this REST API call to delete users with IDs 3, 5, and 7:DELETE /users?ids=3,5,7
What is the expected behavior?
Solution
Step 1: Analyze the query parameter usage
The query parameter 'ids=3,5,7' lists multiple IDs to delete.Step 2: Understand batch delete behavior
The API deletes all specified users in one DELETE request.Final Answer:
Users with IDs 3, 5, and 7 are deleted in one request. -> Option DQuick Check:
Batch delete with multiple IDs = one request deletes all [OK]
- Assuming only first ID is deleted
- Thinking server rejects multiple IDs
- Believing multiple requests are sent automatically
Consider this batch delete request:DELETE /items
with JSON body:{ "ids": [10, 20, 30] }
But the server responds with 400 Bad Request. What is the likely cause?
Solution
Step 1: Check DELETE method usage
Some APIs require IDs in query string, not in request body for DELETE.Step 2: Identify server expectation
Since server returns 400, it likely expects IDs in query parameters, not JSON body.Final Answer:
The server expects IDs in the query string, not body. -> Option BQuick Check:
Server expects IDs in query, not body [OK]
- Assuming DELETE cannot have a body always
- Thinking JSON format is wrong without checking
- Ignoring server API documentation
You want to design a batch delete endpoint for a REST API that deletes multiple products by IDs sent in the request body as JSON. Which approach is best practice?
{
"ids": [101, 102, 103]
}Choose the correct method and request format.
Solution
Step 1: Understand REST conventions for delete
DELETE method is standard for removing resources, including batch deletes.Step 2: Check request body usage
Modern APIs allow DELETE with JSON body to specify multiple IDs for batch delete.Step 3: Evaluate options
POST is not for deletion, GET is for retrieval, query string can be limited in length.Final Answer:
Use DELETE method with JSON body containing IDs array. -> Option AQuick Check:
DELETE + JSON body for batch delete = best practice [OK]
- Using POST instead of DELETE for deletion
- Putting IDs in query string risking URL length limits
- Using GET which is not for deletion
Ready to prove it?
Pass the test and this pattern moves unknown
