PATCH for partial updates in Rest API - Time & Space Complexity
When using PATCH requests to update parts of data, it's important to know how the time to process changes as the data grows.
We want to understand how the server's work changes when updating only some fields.
Analyze the time complexity of the following PATCH request handler.
PATCH /users/{id}
Request body: { "email": "new@example.com" }
// Server-side pseudo-code
user = findUserById(id)
for each field in request body:
user[field] = request body[field]
save user
return updated user
This code updates only the fields sent in the PATCH request for a user.
Look for repeated steps that take time depending on input size.
- Primary operation: Looping over fields in the request body to update user data.
- How many times: Once per field sent in the PATCH request.
The time to update grows with how many fields you send to change.
| Input Size (fields to update) | Approx. Operations |
|---|---|
| 1 | 1 update operation |
| 5 | 5 update operations |
| 20 | 20 update operations |
Pattern observation: The work grows directly with the number of fields you update.
Time Complexity: O(k)
This means the time grows linearly with the number of fields you want to update, not the total data size.
[X] Wrong: "PATCH always takes the same time no matter how many fields are updated."
[OK] Correct: The server must process each field you send, so more fields mean more work and longer time.
Understanding how partial updates scale helps you design efficient APIs and explain your choices clearly in interviews.
"What if the PATCH request included nested objects to update? How would that affect the time complexity?"
Practice
PATCH method in REST APIs?Solution
Step 1: Understand HTTP methods
HTTP methods like GET, POST, PUT, DELETE, and PATCH have different purposes. PATCH is designed for partial updates.Step 2: Identify PATCH behavior
PATCH sends only the fields that need to be changed, unlike PUT which requires the full resource data.Final Answer:
To update only specific fields of a resource without sending the entire data -> Option AQuick Check:
PATCH updates parts only [OK]
- Confusing PATCH with PUT which replaces full resource
- Thinking PATCH creates new resources
- Using PATCH to delete resources
Solution
Step 1: Identify HTTP method and endpoint
PATCH is used to update part of the resource at /users/123.Step 2: Check JSON body for correct field
The JSON body contains only the field to update, here "email" with new value.Final Answer:
PATCH /users/123 {"email": "new@example.com"} -> Option AQuick Check:
PATCH + partial JSON body = correct syntax [OK]
- Using GET or DELETE instead of PATCH
- Sending unrelated fields in PATCH body
- Trying to update username when email is asked
{"price": 19.99}
What will be the result if the original product data was {"name": "Book", "price": 25.00, "stock": 10}?Solution
Step 1: Understand PATCH partial update
PATCH updates only the fields sent, here "price" changes to 19.99.Step 2: Combine unchanged fields with updated field
Fields not in PATCH body remain unchanged, so "name" and "stock" stay the same.Final Answer:
{"name": "Book", "price": 19.99, "stock": 10} -> Option CQuick Check:
PATCH updates price only, others unchanged [OK]
- Replacing entire object instead of partial update
- Removing fields not in PATCH body
- Ignoring updated field in result
{"email": "new@example.com", "age": "twenty"}
The server expects age as an integer. What is the likely problem?Solution
Step 1: Check data types in PATCH body
"age" is sent as string "twenty" but server expects integer.Step 2: Understand server validation
Most servers validate data types and reject requests with invalid types.Final Answer:
The server will reject the request due to invalid data type for age -> Option DQuick Check:
Invalid data type causes rejection [OK]
- Assuming server auto-converts wrong types
- Thinking PATCH ignores invalid fields silently
- Believing PATCH deletes fields on error
title, completed, and priority. You want to mark a task as completed without changing other fields. Which PATCH request body is best?Solution
Step 1: Identify minimal update needed
Only the "completed" field needs to change, so send only that.Step 2: Avoid sending unrelated or empty fields
Sending empty or null fields may overwrite data unintentionally.Final Answer:
{"completed": true} -> Option BQuick Check:
Send only changed fields in PATCH body [OK]
- Sending all fields even if unchanged
- Sending empty or null values accidentally
- Using PUT instead of PATCH for partial update
Ready to prove it?
Pass the test and this pattern moves unknown
