Making GET and POST requests in No-Code - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we make GET or POST requests, the time it takes depends on how many requests we send and how the server handles them.
We want to understand how the total time grows as we increase the number of requests.
Analyze the time complexity of the following code snippet.
for each item in list:
send GET request to server
wait for response
process response
This code sends a GET request for each item in a list, waits for the server to respond, then processes the response.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sending a GET request and waiting for its response.
- How many times: Once for each item in the list.
As the number of items grows, the total time grows roughly in the same way because each item causes one request.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 requests and responses |
| 100 | 100 requests and responses |
| 1000 | 1000 requests and responses |
Pattern observation: The total time grows directly with the number of requests.
Time Complexity: O(n)
This means the time grows in a straight line as you add more requests.
[X] Wrong: "Sending multiple requests at once will always take the same time as sending one."
[OK] Correct: Each request takes time, so more requests usually mean more total time, even if some happen at the same time.
Understanding how request time grows helps you design better apps and explain your thinking clearly in interviews.
"What if we send all requests at the same time without waiting for each response? How would the time complexity change?"
Practice
GET request in web communication?Solution
Step 1: Understand the role of GET requests
GET requests are used to retrieve or ask for data from a server without making any changes.Step 2: Compare with other request types
POST requests send data to the server to create or update information, unlike GET which only reads data.Final Answer:
To ask for data without changing anything on the server -> Option AQuick Check:
GET = Read data [OK]
- Confusing GET with POST as both send data
- Thinking GET changes server data
- Assuming GET deletes data
Solution
Step 1: Identify how POST sends data
POST requests send data inside the request body, which is not shown in the URL.Step 2: Differentiate from GET request data sending
GET requests send data via URL parameters, visible after a question mark, unlike POST.Final Answer:
Including data in the request body, not visible in the URL -> Option DQuick Check:
POST = Data in body [OK]
- Using URL parameters for POST data
- Confusing GET and POST data locations
- Thinking POST data is visible in URL
Solution
Step 1: Understand GET request behavior
GET requests are designed to retrieve data and should not change server data.Step 2: Predict server response to update attempt via GET
Most servers ignore any update attempts sent via GET and just return the requested data.Final Answer:
The server will ignore the update and only send data -> Option AQuick Check:
GET = Read only, no update [OK]
- Assuming GET can update data
- Expecting an error always on wrong method
- Confusing GET with POST behavior
fetch('https://api.example.com/data', {
method: 'GET',
body: JSON.stringify({name: 'Alice'})
})
What is the main problem with this code?Solution
Step 1: Check HTTP method and body usage
GET requests do not support sending a body; any body is ignored by browsers and servers.Step 2: Identify correct method for sending data
To send data in the body, the method should be POST or PUT, not GET.Final Answer:
GET requests should not have a body; data won't be sent -> Option CQuick Check:
GET = No body allowed [OK]
- Adding body to GET requests
- Using wrong HTTP method case
- Thinking JSON.stringify is invalid in fetch
Solution
Step 1: Identify the requirement to keep URL clean
Showing data in the URL bar means using GET, which appends data as URL parameters.Step 2: Choose method that sends data in body and keeps URL unchanged
POST sends data in the request body, so the URL stays clean and data is not visible.Final Answer:
POST, because it sends data in the request body and keeps URL clean -> Option BQuick Check:
POST = Data in body, URL clean [OK]
- Choosing GET to hide data
- Thinking GET can send large secure data
- Confusing URL visibility with request speed
