Filtering by field values in Rest API - Time & Space Complexity
When filtering data by field values in a REST API, we want to know how the time to get results changes as the data grows.
We ask: How does the filtering process scale with more data?
Analyze the time complexity of the following code snippet.
GET /items?color=red
// Server-side pseudo-code
items = database.getAllItems()
filtered = []
for item in items:
if item.color == 'red':
filtered.append(item)
return filtered
This code fetches all items and filters those whose color field equals 'red'.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through all items to check their color.
- How many times: Once for each item in the data set.
As the number of items grows, the server checks each one once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to filter grows in a straight line as the data size grows.
[X] Wrong: "Filtering by a field is instant no matter how many items there are."
[OK] Correct: The server must check each item to see if it matches, so more items mean more work.
Understanding how filtering scales helps you explain API performance and design better data queries.
"What if the data was stored in a database with an index on the color field? How would the time complexity change?"
Practice
Solution
Step 1: Understand filtering purpose
Filtering is used to get only data that matches certain criteria, not to delete or update.Step 2: Identify filtering effect
Filtering by field values returns only entries matching those values.Final Answer:
Returns only the data entries that match the specified field values -> Option DQuick Check:
Filtering = Select matching data [OK]
- Thinking filtering deletes data
- Confusing filtering with updating data
- Assuming filtering returns all data
Solution
Step 1: Recognize query parameter syntax
Filtering uses query parameters after ? with key=value pairs.Step 2: Match correct syntax
/users?age=30 correctly filters users with age 30.Final Answer:
/users?age=30 -> Option AQuick Check:
Query param = ?key=value [OK]
- Using slashes instead of query parameters
- Using incorrect filter syntax like > without API support
- Missing the ? before query parameters
/products?category=books&available=true, what data will the API return?Solution
Step 1: Analyze query parameters
The URL filters products where category equals 'books' and available equals true.Step 2: Determine filtering effect
Both conditions must be met, so only available books are returned.Final Answer:
Only products in the 'books' category that are available -> Option BQuick Check:
Multiple filters = AND condition [OK]
- Ignoring one of the filter conditions
- Assuming filters combine with OR
- Confusing availability true/false
/orders?status=shipped, but the API returns all orders. What is the likely problem?Solution
Step 1: Check API documentation for supported filters
If 'status' is not a supported filter field, the API ignores it and returns all data.Step 2: Evaluate other options
Changing parameter name or case only works if API supports it; trailing slash usually doesn't affect filtering.Final Answer:
The API does not support filtering by 'status' -> Option CQuick Check:
Unsupported filter = no effect [OK]
- Assuming any field can be filtered
- Changing URL format without docs
- Ignoring case sensitivity without confirmation
/employees that supports filtering by department and active status. How would you get all active employees in the 'sales' department?Solution
Step 1: Use correct query parameter syntax
Query parameters are joined by & and use key=value pairs.Step 2: Combine filters for department and active status
/employees?department=sales&active=true correctly filters active sales employees.Final Answer:
/employees?department=sales&active=true -> Option AQuick Check:
Use & to combine filters [OK]
- Using | or ; instead of & to join filters
- Mixing order of parameters incorrectly
- Using invalid separators causing syntax errors
Ready to prove it?
Pass the test and this pattern moves unknown
