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?"
Practice
Solution
Step 1: Understand the GET method
The GET method is used to request data from a server without changing it.Step 2: Identify the purpose of GET in API
GET asks the server to send data back to the client.Final Answer:
It asks the server to send data back. -> Option AQuick Check:
GET = Request data [OK]
- Confusing GET with POST or DELETE methods
- Thinking GET changes data on server
- Mixing up request and response roles
Solution
Step 1: Identify HTTP methods
GET is the method used to request data, so the request line must start with GET.Step 2: Check the request format
The correct format is "GET /path HTTP/1.1" for a GET request.Final Answer:
GET /api/data HTTP/1.1 -> Option CQuick Check:
GET request line starts with GET [OK]
- Using POST, DELETE, or PUT instead of GET
- Wrong HTTP version or missing method
- Confusing request line with response line
{"status":200,"data":{"name":"Alice","age":30}}What does the status code 200 mean?
Solution
Step 1: Understand HTTP status codes
Status code 200 means the request succeeded and the server returned the requested data.Step 2: Match status code to meaning
200 is standard for success; 404 means not found, 500 means server error, 401 means unauthorized.Final Answer:
The request was successful and data is returned. -> Option DQuick Check:
Status 200 = Success [OK]
- Confusing 200 with error codes like 404 or 500
- Thinking 200 means no data returned
- Mixing status codes with data content
Solution
Step 1: Check the URL correctness
If the URL is wrong or misspelled, the server won't respond properly or may not be found.Step 2: Evaluate other options
Using GET instead of POST may cause wrong data but usually gets a response; status 200 means success; valid Authorization helps access.Final Answer:
The API endpoint URL is incorrect or misspelled. -> Option BQuick Check:
Wrong URL = No response [OK]
- Assuming method error causes no response
- Ignoring URL typos
- Confusing status codes with no response
https://api.example.com/users/123. Which is the best way to check if your request worked?Solution
Step 1: Understand success criteria for API requests
A successful GET request returns status 200 and the expected data.Step 2: Evaluate options for correctness
POST method and 404 means not found; 500 means server error; DELETE method is for removing data, not getting it.Final Answer:
Check if the response status code is 200 and the data contains user details. -> Option AQuick Check:
Status 200 + data = success [OK]
- Confusing POST with GET for data retrieval
- Ignoring status codes meaning
- Checking wrong HTTP methods for GET requests
Ready to prove it?
Pass the test and this pattern moves unknown
