Content negotiation in Rest API - Time & Space Complexity
When a server chooses the best format to send data based on what a client asks for, it uses content negotiation.
We want to know how the time to decide the format changes as the number of supported formats grows.
Analyze the time complexity of the following code snippet.
function negotiateContentType(acceptHeader, supportedTypes) {
for (const type of supportedTypes) {
if (acceptHeader.includes(type)) {
return type;
}
}
return supportedTypes[0];
}
This code checks each supported type to find one that matches the client's Accept header and returns it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the list of supported content types.
- How many times: Up to once for each supported type until a match is found.
As the number of supported types grows, the code may check more items before finding a match or giving up.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of checks grows directly with the number of supported types.
Time Complexity: O(n * m)
This means the time to find a matching content type grows linearly with the number of supported types and the length of the Accept header string.
[X] Wrong: "The server checks all supported types every time, so it always takes the same long time."
[OK] Correct: The server stops checking as soon as it finds a match, so sometimes it finishes quickly.
Understanding how content negotiation scales helps you explain how servers handle different client needs efficiently.
"What if the supported types were stored in a set instead of a list? How would the time complexity change?"
Practice
Solution
Step 1: Understand content negotiation role
Content negotiation lets clients tell servers what data format they prefer.Step 2: Identify how clients specify format
Clients use the Accept header to communicate their preferred format.Final Answer:
To allow clients to specify the data format they want using the Accept header -> Option AQuick Check:
Content negotiation = Accept header usage [OK]
- Confusing content negotiation with authentication
- Thinking it compresses data
- Believing it encrypts data
Solution
Step 1: Recall MIME types for common formats
JSON format uses the MIME type 'application/json'.Step 2: Match Accept header to JSON
The correct Accept header for JSON is 'Accept: application/json'.Final Answer:
Accept: application/json -> Option DQuick Check:
JSON MIME type = application/json [OK]
- Using text/html for JSON
- Confusing image/png with data formats
- Using application/xml instead of JSON
Accept: application/xml, application/json;q=0.8Which content type will the server most likely respond with?
Solution
Step 1: Understand quality values (q) in Accept header
The q value shows preference; higher q means higher priority.Step 2: Compare q values for application/xml and application/json
application/xml has no q value, so defaults to 1. application/json has q=0.8, lower priority.Final Answer:
application/xml -> Option AQuick Check:
Higher q or default 1 wins = application/xml [OK]
- Ignoring q values and picking lower priority
- Choosing JSON because it appears second
- Selecting unrelated content types
Solution
Step 1: Understand HTTP 406 status meaning
406 means server cannot provide a response matching the Accept header.Step 2: Link 406 to content negotiation failure
If client requests unsupported format, server responds 406 Not Acceptable.Final Answer:
Client requested a format the server cannot provide -> Option BQuick Check:
406 = unsupported requested format [OK]
- Confusing 406 with authentication errors
- Thinking 406 means server error
- Assuming 406 is about HTTP methods
Solution
Step 1: Understand content negotiation best practice
The server should read the Accept header to decide response format.Step 2: Choose approach matching client preference
Responding with JSON or XML based on Accept header is correct behavior.Final Answer:
Check the Accept header and respond with JSON or XML accordingly -> Option CQuick Check:
Content negotiation = respond per Accept header [OK]
- Ignoring Accept header and hardcoding response
- Sending multiple formats in one response
- Using unrelated custom headers for format choice
Ready to prove it?
Pass the test and this pattern moves unknown
