Interactive API explorers in Rest API - Time & Space Complexity
When using interactive API explorers, it's important to understand how the time to process requests grows as the number of API calls or data size increases.
We want to know how the system handles more requests or larger responses over time.
Analyze the time complexity of the following code snippet.
// Example: Fetching multiple API endpoints sequentially
async function fetchMultipleAPIs(urls) {
const results = [];
for (const url of urls) {
const response = await fetch(url);
const data = await response.json();
results.push(data);
}
return results;
}
This code fetches data from a list of API URLs one by one and collects the results.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop that fetches each API URL sequentially.
- How many times: Once for each URL in the input list.
As the number of URLs increases, the total time to fetch all data grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 fetch calls |
| 100 | 100 fetch calls |
| 1000 | 1000 fetch calls |
Pattern observation: Doubling the number of URLs roughly doubles the total fetch operations and time.
Time Complexity: O(n)
This means the time to complete grows linearly with the number of API calls.
[X] Wrong: "Fetching multiple APIs one after another takes the same time as fetching just one."
[OK] Correct: Each API call adds extra time, so more calls mean more total time, not the same.
Understanding how API calls scale helps you design better systems and explain your reasoning clearly in interviews.
"What if we fetched all API URLs in parallel instead of one by one? How would the time complexity change?"