Challenge - 5 Problems
Master of Multiple Filters
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of filtering with multiple query parameters
Consider a REST API endpoint that filters products by category and price range using query parameters. What is the output when requesting
/products?category=books&min_price=10&max_price=20 if the database contains:- Book A: category=books, price=15
- Book B: category=books, price=25
- Pen: category=stationery, price=5
Rest API
products = [
{"name": "Book A", "category": "books", "price": 15},
{"name": "Book B", "category": "books", "price": 25},
{"name": "Pen", "category": "stationery", "price": 5}
]
filtered = [p for p in products if p["category"] == "books" and 10 <= p["price"] <= 20]
print(filtered)Attempts:
2 left
💡 Hint
Filter by category first, then check price range.
✗ Incorrect
Only 'Book A' matches both category 'books' and price between 10 and 20 inclusive.
🧠 Conceptual
intermediate1:30remaining
Understanding multiple filter parameters in REST APIs
When a REST API endpoint supports multiple filter parameters like
?color=red&size=medium, how are these filters usually combined to select results?Attempts:
2 left
💡 Hint
Think about how multiple conditions narrow down results.
✗ Incorrect
Multiple filter parameters usually narrow results, so they are combined with AND logic.
🔧 Debug
advanced2:30remaining
Identify the error in handling multiple filters
This code snippet tries to filter users by age and city from query parameters but returns incorrect results. What is the error?
Rest API
def filter_users(users, age=None, city=None): filtered = users if age: filtered = [u for u in users if u['age'] == age] if city: filtered = [u for u in users if u['city'] == city] return filtered users = [ {'name': 'Alice', 'age': 30, 'city': 'NY'}, {'name': 'Bob', 'age': 25, 'city': 'LA'}, {'name': 'Carol', 'age': 30, 'city': 'LA'} ] result = filter_users(users, age=30, city='LA') print(result)
Attempts:
2 left
💡 Hint
Check how the filtered list is updated after each condition.
✗ Incorrect
Each filter uses the original users list instead of the filtered list, so the second filter overwrites the first.
📝 Syntax
advanced1:30remaining
Syntax error in parsing multiple filter parameters
Which option correctly parses multiple filter parameters from a URL query string in Python?
Rest API
from urllib.parse import parse_qs query = 'type=fruit&type=vegetable&color=red' params = parse_qs(query) print(params)
Attempts:
2 left
💡 Hint
parse_qs returns lists for each key.
✗ Incorrect
parse_qs returns a dictionary where each key maps to a list of values, even if only one value exists.
🚀 Application
expert3:00remaining
Combining multiple filters with optional parameters
You want to build a REST API endpoint that filters orders by optional parameters:
status, min_total, and max_total. Which code snippet correctly applies all filters only if parameters are provided?Attempts:
2 left
💡 Hint
Apply filters step by step on the current filtered list.
✗ Incorrect
Option C correctly filters the list cumulatively, applying each filter only if the parameter is given.