Multiple filter parameters in Rest API - Time & Space Complexity
When using multiple filter parameters in a REST API, it's important to understand how the processing time changes as the number of filters or data size grows.
We want to know how the time to handle requests grows when more filters or data are involved.
Analyze the time complexity of the following code snippet.
GET /items?color=red&size=medium&brand=acme
// Server-side pseudo-code
function getFilteredItems(filters) {
let results = allItems;
for (const key in filters) {
results = results.filter(item => item[key] === filters[key]);
}
return results;
}
This code filters a list of items by applying each filter parameter one after another.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Filtering the list of items for each filter parameter.
- How many times: Once per filter parameter, each time scanning the current list of results.
As the number of items or filters increases, the filtering work grows because each filter scans the current list.
| Input Size (n items) | Approx. Operations (with k filters) |
|---|---|
| 10 | About 10 x k |
| 100 | About 100 x k |
| 1000 | About 1000 x k |
Pattern observation: The total work grows roughly in a straight line with the number of items and filters.
Time Complexity: O(n x k)
This means the time to process grows proportionally with both the number of items and the number of filters.
[X] Wrong: "Adding more filters does not affect performance much because each filter is simple."
[OK] Correct: Each filter scans the current list, so more filters mean more repeated scanning, increasing total work.
Understanding how multiple filters affect performance helps you design efficient APIs and explain trade-offs clearly in interviews.
"What if the filters were combined into a single pass instead of multiple passes? How would the time complexity change?"
Practice
example.com/api/items?color=red&size=mediumSolution
Step 1: Understand multiple filters in URL
Multiple filters joined by&mean the server should apply all conditions together.Step 2: Interpret the example URL
The URL?color=red&size=mediummeans items must be red AND medium size.Final Answer:
Returns items that match all filter conditions (color red AND size medium). -> Option AQuick Check:
Multiple filters = AND condition [OK]
- Thinking filters work as OR instead of AND
- Assuming filters are ignored
- Believing multiple filters cause errors
category=books and author=doe in a REST API URL?Solution
Step 1: Identify correct separator for multiple filters
In URLs, multiple query parameters are separated by&.Step 2: Check each option
Only example.com/api/items?category=books&author=doe uses&correctly between filters.Final Answer:
example.com/api/items?category=books&author=doe -> Option BQuick Check:
Use & to separate filters [OK]
- Using | or ; instead of &
- Leaving spaces between parameters
- Not using any separator
GET /products?color=blue&size=large&in_stock=trueWhat kind of products will the server return?
Solution
Step 1: Understand multiple filters joined by &
Filters combined with&mean all conditions must be true together.Step 2: Apply to given filters
Only products that are blue, large, and in stock will be returned.Final Answer:
Products that are blue AND large AND in stock. -> Option AQuick Check:
Multiple filters = AND condition [OK]
- Thinking filters combine with OR
- Ignoring some filters
- Assuming filters are optional
age=30 and active=true:example.com/api/users?age=30&active=true&But the server returns an error. What is the likely problem?
Solution
Step 1: Check URL syntax for filters
A trailing&at the end of query parameters is invalid syntax.Step 2: Understand server error cause
The server likely rejects the URL because of the extra&with no following parameter.Final Answer:
The trailing & at the end causes a syntax error. -> Option DQuick Check:
Trailing & causes error [OK]
- Leaving trailing & at end of URL
- Using commas instead of &
- Assuming quotes are needed around values
status is shipped and total is greater than 100. The API supports filtering by multiple parameters but only exact matches. How can you combine filters to get orders with status=shipped and total>100?Solution
Step 1: Understand API filter limitations
The API only supports exact matches, so direct > operator is not allowed.Step 2: Use API-supported range filter parameter
Many APIs use parameters liketotal_minto filter values greater than a number.Step 3: Combine filters correctly
Using?status=shipped&total_min=101filters orders with status shipped and total greater than 100.Final Answer:
Use ?status=shipped&total_min=101 if the API supports range filters. -> Option CQuick Check:
Use API range params for > filters [OK]
- Trying to use > directly in URL
- Assuming all APIs support gt: syntax
- Filtering manually after fetching all data
Ready to prove it?
Pass the test and this pattern moves unknown
