Query parameter versioning in Rest API - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using query parameter versioning in REST APIs, it's important to understand how the time to handle requests changes as the number of versions grows.
We want to know how the server's work increases when it checks the version from the query parameter.
Analyze the time complexity of the following code snippet.
// Example of query parameter versioning
app.get('/api/resource', (req, res) => {
const version = req.query.v;
if (version === '1') {
// handle version 1
} else if (version === '2') {
// handle version 2
} else {
// default handler
}
res.send('response');
});
This code checks the version number from the query parameter and chooses the matching handler.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking the version value against each supported version in sequence.
- How many times: Once per request, but the number of checks grows with the number of versions.
As the number of supported versions increases, the server checks more conditions to find the right handler.
| Number of Versions (n) | Approx. Checks |
|---|---|
| 2 | 2 checks |
| 10 | 10 checks |
| 100 | 100 checks |
Pattern observation: The number of checks grows directly with the number of versions.
Time Complexity: O(n)
This means the time to find the correct version handler grows linearly as more versions are added.
[X] Wrong: "Checking the version is always constant time because it's just one query parameter."
[OK] Correct: When many versions exist, the server must check each version condition one by one, so time grows with the number of versions.
Understanding how version checks scale helps you design APIs that stay fast as they grow. This skill shows you can think about performance beyond just making code work.
"What if we replaced the if-else checks with a direct lookup in a dictionary mapping versions to handlers? How would the time complexity change?"
Practice
Solution
Step 1: Understand query parameter versioning concept
Query parameter versioning lets clients add a version number in the URL query string, like?version=1.Step 2: Identify the purpose of this method
This allows clients to choose which API version to use without changing the main URL path.Final Answer:
To allow clients to specify which API version they want using a query string -> Option BQuick Check:
Query parameter versioning = client selects version [OK]
- Thinking versioning changes the main URL path
- Assuming versioning hides API versions
- Believing all clients must use latest version only
Solution
Step 1: Recall query parameter syntax in URLs
Query parameters come after a question mark?and use key=value pairs, e.g.,?version=2.Step 2: Match the correct syntax for versioning
The correct way to specify version 2 is by adding?version=2after the resource path.Final Answer:
GET /api/resource?version=2 -> Option CQuick Check:
Query param syntax = ?key=value [OK]
- Using slashes instead of query parameters
- Using # instead of ? for query parameters
- Placing version inside the URL path incorrectly
def get_data(request):
version = request.GET.get('version', '1')
if version == '1':
return 'Data from v1'
elif version == '2':
return 'Data from v2'
else:
return 'Unknown version'What will be the output if the client calls
/api/data?version=2?Solution
Step 1: Extract version from query parameters
The code gets the version from the query string. If missing, it defaults to '1'. Here, version='2'.Step 2: Check version conditions
Since version is '2', the code returns 'Data from v2'.Final Answer:
"Data from v2" -> Option AQuick Check:
version=2 returns v2 data [OK]
- Assuming default version is always used
- Confusing string and integer comparison
- Expecting error when version is provided
def api_handler(request):
version = request.GET['version']
if version == '1':
return 'Version 1'
else:
return 'Other version'What error will occur if the client calls
/api/data without a version parameter?Solution
Step 1: Understand how version is accessed
The code usesrequest.GET['version']which raises KeyError if 'version' is missing.Step 2: Analyze call without version parameter
Calling without?version=...means 'version' key is missing, causing KeyError.Final Answer:
KeyError because 'version' key is missing -> Option AQuick Check:
Missing key access causes KeyError [OK]
- Assuming default version is used automatically
- Expecting no error when parameter is missing
- Confusing KeyError with SyntaxError
Solution
Step 1: Understand query parameter versioning usage
Clients specify version using?version=1or?version=2in the same URL.Step 2: Return different data formats based on version
Return simple list of strings for version 1, and detailed objects for version 2, matching client request.Final Answer:
Use ?version=1 to return ['Alice', 'Bob'], and ?version=2 to return [{'name':'Alice','id':1},{'name':'Bob','id':2}] -> Option DQuick Check:
Query param version controls response format [OK]
- Changing URL path instead of using query parameters
- Ignoring version parameter and mixing formats
- Returning errors unnecessarily for missing versions
