GET for reading resources in Rest API - Time & Space Complexity
When we use a GET request to read data from a server, it's important to understand how the time it takes grows as the amount of data grows.
We want to know: how does the work change when we ask for more or less data?
Analyze the time complexity of the following code snippet.
GET /items
// Server fetches all items from database
// Returns list of items as response
This code handles a GET request to read all items stored on the server and sends them back to the client.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The server reads each item from the database one by one.
- How many times: Once for each item stored (n times).
As the number of items grows, the server must read more data, so the work grows in a straight line with the number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 reads |
| 100 | 100 reads |
| 1000 | 1000 reads |
Pattern observation: Doubling the items doubles the work needed.
Time Complexity: O(n)
This means the time to complete the GET request grows directly with the number of items requested.
[X] Wrong: "GET requests always take the same time no matter how much data is requested."
[OK] Correct: The server must read and send each item, so more items mean more work and more time.
Understanding how GET requests scale helps you explain how APIs handle data and what to expect when data grows, a useful skill in many programming tasks.
"What if the GET request only returned a fixed number of items regardless of total data size? How would the time complexity change?"
Practice
GET request in REST API?Solution
Step 1: Understand the HTTP methods
GET is one of the HTTP methods used in REST APIs. It is specifically designed to retrieve data without changing it.Step 2: Identify the purpose of GET
GET requests fetch or read data from the server without modifying it, unlike POST, PUT, or DELETE.Final Answer:
To read or fetch data from the server -> Option AQuick Check:
GET = Read data [OK]
- Confusing GET with POST which creates data
- Thinking GET deletes data
- Assuming GET updates data
Solution
Step 1: Identify the HTTP method for reading
GET is the correct method to read or fetch data, so the method must be GET.Step 2: Check the URL path for resource
/users/5 is the typical RESTful path to get a user with ID 5. Other options use wrong HTTP methods.Final Answer:
GET /users/5 -> Option CQuick Check:
GET + resource path = correct syntax [OK]
- Using POST or DELETE instead of GET
- Using query parameters incorrectly
- Mixing HTTP methods and URLs
/products?category=books&limit=3, what does the server most likely return?Solution
Step 1: Analyze the query parameters
The URL has two query parameters: category=books and limit=3, which filter products by category and limit the number returned.Step 2: Understand typical API behavior
APIs commonly use 'category' to filter items and 'limit' to restrict the number of results returned.Final Answer:
First 3 products in the books category -> Option DQuick Check:
Query filters + limit = filtered limited results [OK]
- Ignoring query parameters and returning all data
- Misreading 'limit' as a category
- Assuming query causes error
GET /users/ with body {"id": 10}Solution
Step 1: Understand GET request rules
GET requests do not send a body; they pass parameters via URL path or query string.Step 2: Check the given request
The request has a JSON body, which is invalid for GET and ignored by most servers.Final Answer:
GET requests should not have a body -> Option BQuick Check:
GET = no body allowed [OK]
- Adding body data to GET requests
- Confusing GET with POST or PUT
- Assuming URL must always include ID
Solution
Step 1: Choose correct HTTP method and URL format
GET is used to read data. Query parameters are the standard way to filter resources in GET requests.Step 2: Analyze options for filtering
GET /users?status=active&created_after=2023-01-01 uses query parameters to filter by status and creation date, which is the correct RESTful approach.Final Answer:
GET /users?status=active&created_after=2023-01-01 -> Option AQuick Check:
GET + query filters = correct usage [OK]
- Using POST instead of GET for reading
- Putting filters in URL path incorrectly
- Sending body with GET request
Ready to prove it?
Pass the test and this pattern moves unknown
