Media type versioning in Rest API - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using media type versioning in REST APIs, it's important to understand how the server handles requests with different versions.
We want to know how the processing time changes as the number of versions or requests grows.
Analyze the time complexity of the following code snippet.
GET /api/resource HTTP/1.1
Accept: application/vnd.example.v1+json
// Server checks the Accept header
switch (version) {
case 'v1': return dataV1;
case 'v2': return dataV2;
default: return error;
}
This code checks the version requested in the Accept header and returns the matching data format.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking the version string against known versions.
- How many times: Once per request, with a small fixed number of versions to check.
As the number of requests grows, the server checks the version once per request.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 requests | 10 version checks |
| 100 requests | 100 version checks |
| 1000 requests | 1000 version checks |
Pattern observation: The work grows directly with the number of requests, but each check is simple and fixed.
Time Complexity: O(n)
This means the time to handle requests grows linearly with the number of requests.
[X] Wrong: "Checking the version header takes longer as more versions exist because it loops through all versions."
[OK] Correct: Usually, the server uses a simple switch or map lookup, so checking a version is a quick fixed step, not a loop over all versions.
Understanding how version checks scale helps you explain API design choices clearly and shows you can think about performance in real systems.
"What if the server supported hundreds of versions and checked them by searching a list? How would the time complexity change?"
Practice
Solution
Step 1: Understand media type versioning concept
Media type versioning lets clients request specific API versions by setting custom content types in headers.Step 2: Identify the main purpose
This approach helps keep APIs backward compatible by allowing multiple versions to coexist.Final Answer:
To allow clients to specify API versions via custom content types -> Option AQuick Check:
Media type versioning = clients specify versions [OK]
- Confusing versioning with encryption
- Thinking URL changes are required
- Assuming it improves speed directly
Solution
Step 1: Identify header for version negotiation
The client uses theAcceptheader to tell the server which media type and version it wants.Step 2: Differentiate from other headers
Content-Typeis for request body format,Authorizationis for credentials, andUser-Agentidentifies the client software.Final Answer:
Accept -> Option BQuick Check:
Version in Accept header [OK]
- Using Content-Type instead of Accept for versioning
- Confusing Authorization with version info
- Thinking User-Agent controls version
Accept: application/vnd.example.v2+jsonWhat version of the API is the client requesting?
Solution
Step 1: Parse the media type string
The media type isapplication/vnd.example.v2+json. The.v2part indicates version 2.Step 2: Confirm version number meaning
Thev2suffix is a common pattern to specify API version 2 in media type versioning.Final Answer:
Version 2 -> Option CQuick Check:
v2 in media type means version 2 [OK]
- Ignoring the .v2 and picking version 1
- Confusing +json suffix as version
- Assuming no version if not in URL
Accept: application/vnd.example.v1+jsonBut the server responds with version 2 data. What is the likely cause?
Solution
Step 1: Analyze client request and server response
The client requests version 1 via Accept header, but server returns version 2 data.Step 2: Identify server behavior
This usually means the server ignores the Accept header and serves the latest version by default.Final Answer:
Server ignores Accept header and defaults to latest version -> Option DQuick Check:
Server ignoring Accept header causes version mismatch [OK]
- Blaming client Content-Type instead of Accept
- Assuming Accept header syntax error without checking
- Thinking server lacks version 2 support
Solution
Step 1: Understand requirement for simultaneous version support
To support multiple versions, the server must detect which version the client wants.Step 2: Identify correct server behavior
Parsing the Accept header and returning matching version data allows backward compatibility and coexistence.Final Answer:
Parse the Accept header and return data matching the requested version -> Option AQuick Check:
Server parses Accept header to serve requested version [OK]
- Ignoring Accept header and forcing one version
- Switching to URL versioning instead of media type
- Returning errors instead of supporting old versions
