Bulk import and export in Rest API - Time & Space Complexity
When working with bulk import and export in REST APIs, it's important to understand how the time to process data grows as the amount of data increases.
We want to know how the number of operations changes when we import or export many items at once.
Analyze the time complexity of the following code snippet.
POST /api/items/bulk-import
Request body: [item1, item2, ..., itemN]
for each item in request body:
validate(item)
save_to_database(item)
Response: success or error
This code receives a list of items to import, then validates and saves each item one by one.
- Primary operation: Looping through each item to validate and save.
- How many times: Once for each item in the input list (N times).
As the number of items increases, the total work grows proportionally.
| 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 input roughly doubles the work done.
Time Complexity: O(n)
This means the time to complete the import grows directly with the number of items.
[X] Wrong: "Bulk import runs in constant time because it is one API call."
[OK] Correct: Even though it's one call, the server still processes each item individually, so time grows with the number of items.
Understanding how bulk operations scale helps you design efficient APIs and explain performance clearly in real projects.
"What if the API validated all items in parallel instead of one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand bulk import action
Bulk import means sending many new records to the server at once.Step 2: Identify HTTP method for creating resources
POST is used to send data to create new records in REST APIs.Final Answer:
POST -> Option BQuick Check:
Bulk import uses POST [OK]
- Choosing GET which is for fetching data
- Using DELETE which removes data
- Confusing PUT with bulk import
Solution
Step 1: Recognize bulk import JSON structure
Bulk import expects a list (array) of objects, each representing a record.Step 2: Check each option format
[{"name": "Alice"}, {"name": "Bob"}] is a JSON array with two objects, each with a "name" key. Others are invalid or wrong format.Final Answer:
[{"name": "Alice"}, {"name": "Bob"}] -> Option AQuick Check:
Bulk import uses JSON array of objects [OK]
- Using a single object with duplicate keys
- Sending a string instead of objects
- Using a simple list of strings without keys
{"users": [{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]}What is the value of the second user's name?
Solution
Step 1: Locate the users array in the JSON
The key "users" holds a list of user objects.Step 2: Identify the second user object and its name
The second object is {"id":2,"name":"Bob"}, so the name is "Bob".Final Answer:
Bob -> Option DQuick Check:
Second user's name = Bob [OK]
- Confusing index and picking first user
- Selecting the id instead of name
- Choosing the key name instead of value
[{"name": "Alice"}, {"name": "Bob"}But the server returns a 400 Bad Request error. What is the likely problem?
Solution
Step 1: Check JSON syntax
The JSON is missing the closing bracket "]" for the array.Step 2: Understand server error meaning
400 Bad Request often means invalid JSON syntax was sent.Final Answer:
Missing closing bracket in JSON array -> Option AQuick Check:
Invalid JSON syntax causes 400 error [OK]
- Assuming wrong HTTP method caused error
- Thinking data types must be numbers
- Believing XML is required instead of JSON
Solution
Step 1: Identify correct HTTP method for export
Bulk export uses GET to retrieve data from the server.Step 2: Use query parameters for filtering
Filtering by price is done via query string like ?min_price=100.Final Answer:
Send a GET request to /products?min_price=100 and parse the returned list -> Option CQuick Check:
Bulk export uses GET with filters in query [OK]
- Using POST for export instead of GET
- Sending filters in request body for GET
- Using DELETE or PUT incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
