Batch create endpoint design in Rest API - Time & Space Complexity
When designing a batch create endpoint, it is important to understand how the time to process requests grows as the number of items increases.
We want to know how the server's work changes when more items are sent to be created at once.
Analyze the time complexity of the following code snippet.
POST /items/batch
Request Body: [item1, item2, item3, ..., itemN]
for each item in request body:
validate(item)
save item to database
return success response
This code receives a list of items, validates each one, saves them one by one, and then returns a success message.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each item to validate and save it.
- How many times: Exactly once per item in the batch.
As the number of items increases, the total work grows in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 validations and saves |
| 100 | About 100 validations and saves |
| 1000 | About 1000 validations and saves |
Pattern observation: Doubling the number of items roughly doubles the work done.
Time Complexity: O(n)
This means the time to process grows linearly with the number of items sent in the batch.
[X] Wrong: "Processing multiple items at once will always take the same time as processing one item."
[OK] Correct: Each item requires its own validation and save, so more items mean more work and more time.
Understanding how batch operations scale helps you design efficient APIs and explain your reasoning clearly in interviews.
"What if the endpoint used a bulk database insert instead of saving items one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand HTTP methods for data creation
POST is used to create new resources on the server.Step 2: Apply to batch create context
Batch create sends multiple new items in one POST request.Final Answer:
POST -> Option BQuick Check:
Batch create uses POST [OK]
- Using GET which is for reading data
- Using DELETE which removes data
- Using PUT which replaces resources
Solution
Step 1: Identify JSON array structure for batch data
Batch create expects an object with a key containing an array of items.Step 2: Match correct JSON format
{"items": [{"name": "item1"}, {"name": "item2"}]} is a JSON object with an array of objects, which is correct for batch create.Final Answer:
{"items": [{"name": "item1"}, {"name": "item2"}]} -> Option CQuick Check:
Batch create body = JSON object with items array [OK]
- Using duplicate keys in one object
- Sending a single object instead of an array
- Using incorrect JSON syntax
POST /items/batch
Request body: [{"name": "item1"}, {"name": ""}]
Response: 400 Bad Request
{
"error": "Validation failed",
"details": [{"index": 1, "message": "Name cannot be empty"}]
}Solution
Step 1: Understand validation behavior in batch create
If any item is invalid, the server should reject the whole batch to keep data consistent.Step 2: Analyze the response code and message
Response 400 with error details means no items were created and client is informed.Final Answer:
No items are created, and error details are returned -> Option DQuick Check:
Validation failure returns 400 with errors [OK]
- Assuming partial success without error
- Ignoring error details in response
- Expecting server to crash on bad input
Solution
Step 1: Identify cause of 500 error on empty input
Server likely crashes because it does not handle empty arrays properly.Step 2: Implement input validation for empty arrays
Return 400 Bad Request to inform client that empty batch is invalid.Final Answer:
Add validation to return 400 Bad Request if array is empty -> Option AQuick Check:
Validate input to avoid server errors [OK]
- Ignoring empty input causing crashes
- Changing HTTP method incorrectly
- Skipping JSON parsing
Solution
Step 1: Understand partial success reporting
To report success or failure per item, response must include status and details for each.Step 2: Match response format to requirements
{"results": [{"id": 1, "status": "created"}, {"error": "Invalid name", "status": "failed"}]} provides per-item status and error messages, allowing partial success.Final Answer:
{"results": [{"id": 1, "status": "created"}, {"error": "Invalid name", "status": "failed"}]} -> Option AQuick Check:
Per-item status in response enables partial success [OK]
- Failing whole batch on one error
- Returning only success without error info
- Using generic messages without details
Ready to prove it?
Pass the test and this pattern moves unknown
