Batch update patterns in Rest API - Time & Space Complexity
When updating many items at once using a batch update, it is important to understand how the time needed grows as the number of items increases.
We want to know how the total work changes when we update more records in one go.
Analyze the time complexity of the following code snippet.
POST /api/items/batch-update
Request Body: { "items": [ {"id":1, "value":"A"}, {"id":2, "value":"B"}, ... ] }
// Server side pseudo-code
for item in request.items:
update_database(item.id, item.value)
return success
This code updates each item in the batch one by one in the database.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over each item to update it in the database.
- How many times: Once for every item in the batch.
As the number of items to update grows, the total work grows in a similar way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 updates |
| 100 | 100 updates |
| 1000 | 1000 updates |
Pattern observation: Doubling the number of items roughly doubles the work needed.
Time Complexity: O(n)
This means the time to complete the batch update grows directly in proportion to the number of items.
[X] Wrong: "Batch updating many items is always constant time because it's one request."
[OK] Correct: Even though it's one request, the server still processes each item one by one, so time grows with the number of items.
Understanding how batch updates scale helps you design efficient APIs and explain your choices clearly in interviews.
"What if the server used a single database query to update all items at once? How would the time complexity change?"
Practice
Solution
Step 1: Understand batch update purpose
Batch update patterns let you update many items at once instead of one by one.Step 2: Identify the benefit
This reduces the number of requests and saves time and server resources.Final Answer:
They allow updating multiple items with a single request, saving time. -> Option BQuick Check:
Batch update = single request for many items [OK]
- Thinking batch updates increase requests
- Confusing batch update with single item update
- Assuming batch updates use GET method
Solution
Step 1: Recall HTTP methods for updates
PATCH is used to partially update resources, suitable for batch updates.Step 2: Eliminate other methods
GET retrieves data, POST creates new data, DELETE removes data, so they are not best for batch updates.Final Answer:
PATCH -> Option CQuick Check:
Batch update uses PATCH method [OK]
- Using GET for updates
- Confusing POST with update method
- Using DELETE instead of PATCH
[{"id":1,"status":"done"},{"id":2,"status":"pending"}]What does this payload represent?
Solution
Step 1: Analyze JSON payload
The payload is an array of objects each with 'id' and 'status' fields, indicating updates.Step 2: Understand PATCH semantics
PATCH updates existing items partially, so this changes status fields for items with id 1 and 2.Final Answer:
Update status of items with id 1 and 2 to 'done' and 'pending' respectively. -> Option DQuick Check:
PATCH with array updates multiple items [OK]
- Thinking PATCH creates new items
- Confusing update with delete
- Assuming it retrieves data
[{"id":1,"status":"done"}, {"status":"pending"}]What is the likely problem?
Solution
Step 1: Check each object in payload
The second object lacks 'id', so the server cannot identify which item to update.Step 2: Understand update requirements
Each update must specify the item to update, usually by 'id'. Missing it causes failure or error.Final Answer:
Missing 'id' in second object causes update failure or error. -> Option AQuick Check:
Each update needs an identifier like 'id' [OK]
- Assuming missing 'id' is allowed
- Confusing PATCH with POST errors
- Thinking empty arrays are required
Solution
Step 1: Identify filtering need
Only active users should be updated, so filter users before sending batch update.Step 2: Choose correct method and payload
PATCH with an array of filtered user objects updates roles efficiently in one request.Final Answer:
Send a PATCH request with an array of user objects filtered to active users only. -> Option AQuick Check:
Filter then batch update with PATCH [OK]
- Sending updates for all users without filtering
- Using GET or DELETE for updates
- Sending multiple separate requests instead of batch
Ready to prove it?
Pass the test and this pattern moves unknown
