First API request and response in Rest API - Time & Space Complexity
When we send an API request and get a response, it is important to know how long this process takes as the data grows.
We want to understand how the time to get a response changes when the request asks for more data.
Analyze the time complexity of the following code snippet.
GET /users
// Server fetches all users from database
// Then sends the list back as response
This code sends a request to get all users and waits for the server to return the full list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Server reads each user record from the database.
- How many times: Once for each user in the database.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Reads 10 user records |
| 100 | Reads 100 user records |
| 1000 | Reads 1000 user records |
Pattern observation: The time grows directly with the number of users requested.
Time Complexity: O(n)
This means the time to get the response grows in a straight line as the number of users increases.
[X] Wrong: "The response time stays the same no matter how many users there are."
[OK] Correct: Because the server must read each user to include it in the response, more users mean more work and longer time.
Understanding how request time grows with data size helps you explain API performance clearly and shows you think about real user experience.
"What if the API only returned the first 10 users regardless of total users? How would the time complexity change?"