Composite operations (multi-resource) in Rest API - Time & Space Complexity
When a REST API performs multiple resource calls in one operation, the total time depends on all those calls combined.
We want to know how the total work grows as we add more resources to handle.
Analyze the time complexity of the following code snippet.
async function compositeOperation(resources) {
const results = [];
for (const resource of resources) {
const response = await fetch(`/api/${resource}`);
const data = await response.json();
results.push(data);
}
return results;
}
This code fetches data from multiple API endpoints one after another and collects the results.
- Primary operation: Fetching each resource from the API.
- How many times: Once for each resource in the input list.
Each additional resource adds one more fetch call, so the total work grows directly with the number of resources.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 fetch calls |
| 100 | 100 fetch calls |
| 1000 | 1000 fetch calls |
Pattern observation: The total work increases in a straight line as more resources are added.
Time Complexity: O(n)
This means the total time grows proportionally with the number of resources we fetch.
[X] Wrong: "Fetching multiple resources at once is always faster and takes constant time."
[OK] Correct: Each resource still requires a separate fetch call, so total time grows with how many resources there are, even if done sequentially.
Understanding how multiple API calls add up helps you design efficient systems and explain your reasoning clearly in interviews.
"What if we changed the code to fetch all resources in parallel instead of one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand what composite operations do
Composite operations bundle several API calls into one request to reduce network overhead.Step 2: Identify the main benefit
This bundling saves time and ensures related data changes happen together, keeping consistency.Final Answer:
They combine multiple API calls into a single request to save time and keep data consistent. -> Option AQuick Check:
Composite operations = combine calls + consistency [OK]
- Confusing composite operations with documentation tools
- Thinking composite operations encrypt data
- Assuming composite operations run SQL queries
Solution
Step 1: Identify the correct JSON structure for composite operations
Composite operations usually use an "operations" array with each operation having "method" and "path" keys.Step 2: Compare options
{"operations": [{"method": "POST", "path": "/users"}, {"method": "PUT", "path": "/accounts/123"}]} matches this structure exactly, while others use incorrect keys or formats.Final Answer:
{"operations": [{"method": "POST", "path": "/users"}, {"method": "PUT", "path": "/accounts/123"}]} -> Option DQuick Check:
Composite request = "operations" array with method and path [OK]
- Using singular "operation" instead of plural "operations"
- Using wrong keys like "url" or "endpoint"
- Not wrapping operations inside an array
{
"operations": [
{"method": "POST", "path": "/users", "body": {"name": "Alice"}},
{"method": "POST", "path": "/orders", "body": {"userId": "${1.id}", "item": "Book"}}
]
}
What is the expected behavior?Solution
Step 1: Understand operation order and referencing
The first operation creates a user named Alice. The second operation uses "${1.id}" to reference the ID from the first operation's result.Step 2: Determine expected behavior
This means the order is created linked to the new user. This chaining is a key feature of composite operations.Final Answer:
Create a user named Alice, then create an order linked to that user using the new user ID. -> Option AQuick Check:
Reference previous result with ${1.id} = chained operations [OK]
- Assuming operations run in parallel without order
- Thinking referencing previous results is invalid
- Confusing operation indexes starting at 0 instead of 1
{
"operations": [
{"method": "POST", "path": "/products", "body": {"name": "Pen"}},
{"method": "PUT", "path": "/inventory/${1.id}", "body": {"stock": 100}}
]
}
But the API returns an error saying "Invalid reference ${1.id}". What is the likely cause?Solution
Step 1: Check the reference usage
The error "Invalid reference ${1.id}" means the first operation's response does not contain an "id" field.Step 2: Understand why this causes error
If the first operation's response lacks "id", the reference ${1.id} is invalid and causes failure.Final Answer:
The first operation did not return an ID, so ${1.id} is invalid. -> Option BQuick Check:
Reference error = missing id in first operation response [OK]
- Assuming index starts at 0 instead of 1
- Thinking PUT is disallowed in composite operations
- Ignoring that referenced fields must exist in response
Solution
Step 1: Identify correct operation order and referencing
The customer creation is first (operation 1), so its ID is referenced as ${1.id} in subsequent operations.Step 2: Verify all references use ${1.id} and order is logical
Orders and update use ${1.id} correctly. { "operations": [ {"method": "POST", "path": "/customers", "body": {"name": "Bob"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${1.id}", "item": "Laptop"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${1.id}", "item": "Mouse"}}, {"method": "PUT", "path": "/customers/${1.id}", "body": {"status": "active"}} ] } matches this perfectly. { "operations": [ {"method": "POST", "path": "/customers", "body": {"name": "Bob"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${2.id}", "item": "Laptop"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${2.id}", "item": "Mouse"}}, {"method": "PUT", "path": "/customers/${2.id}", "body": {"status": "active"}} ] } incorrectly uses ${2.id} which refers to the first order, not the customer.Final Answer:
{ "operations": [ {"method": "POST", "path": "/customers", "body": {"name": "Bob"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${1.id}", "item": "Laptop"}}, {"method": "POST", "path": "/orders", "body": {"customerId": "${1.id}", "item": "Mouse"}}, {"method": "PUT", "path": "/customers/${1.id}", "body": {"status": "active"}} ] } -> Option CQuick Check:
Use ${1.id} to reference first operation's customer ID [OK]
- Referencing wrong operation index for customer ID
- Hardcoding IDs instead of referencing dynamically
- Incorrect operation order breaking dependencies
Ready to prove it?
Pass the test and this pattern moves unknown
