Example requests and responses in Rest API - Time & Space Complexity
We want to understand how the time to handle API requests changes as we get more requests.
How does the server's work grow when it processes many example requests and responses?
Analyze the time complexity of the following code snippet.
GET /items
Response: List all items
POST /items
Request: Add one item
Response: Confirmation
GET /items/{id}
Response: One item details
PUT /items/{id}
Request: Update one item
Response: Confirmation
DELETE /items/{id}
Response: Confirmation
This code shows example API requests and responses for managing items in a system.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Handling each API request one by one.
- How many times: Once per request received.
Each request is handled separately, so the work grows directly with the number of requests.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 requests | 10 operations |
| 100 requests | 100 operations |
| 1000 requests | 1000 operations |
Pattern observation: The work grows in a straight line as requests increase.
Time Complexity: O(n)
This means the time to handle requests grows directly with how many requests come in.
[X] Wrong: "Handling one request takes the same time no matter how many requests come in."
[OK] Correct: While one request is handled individually, the total time grows as more requests arrive because each needs processing.
Understanding how request handling scales helps you explain server performance clearly and confidently.
"What if the server handled multiple requests at the same time? How would the time complexity change?"