0
0
Rest APIprogramming~20 mins

Multiple filter parameters in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Multiple Filters
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[{'name': 'Pen', 'category': 'stationery', 'price': 5}]
B[{'name': 'Book B', 'category': 'books', 'price': 25}]
C[]
D[{'name': 'Book A', 'category': 'books', 'price': 15}]
Attempts:
2 left
💡 Hint
Filter by category first, then check price range.
🧠 Conceptual
intermediate
1: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?
AFilters are combined with AND logic, so results must match all filters.
BFilters are combined with OR logic, so results can match any filter.
CFilters are ignored and all results are returned.
DFilters are combined randomly for each request.
Attempts:
2 left
💡 Hint
Think about how multiple conditions narrow down results.
🔧 Debug
advanced
2: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)
AThe second filter overwrites the first, so only city filter applies.
BThe function returns an empty list because users is empty.
CThe function raises a TypeError due to wrong parameter types.
DThe function filters correctly and returns both conditions.
Attempts:
2 left
💡 Hint
Check how the filtered list is updated after each condition.
📝 Syntax
advanced
1: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)
A{'type': 'vegetable', 'color': 'red'}
B{'type': ['fruit', 'vegetable'], 'color': ['red']}
CSyntaxError: invalid syntax
D{'type': ['fruit'], 'color': ['red']}
Attempts:
2 left
💡 Hint
parse_qs returns lists for each key.
🚀 Application
expert
3: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?
A
filtered = []
for o in orders:
  if status and o['status'] != status:
    continue
  if min_total and o['total'] &lt; min_total:
    continue
  if max_total and o['total'] &gt; max_total:
    continue
  filtered.append(o)
B
filtered = orders
if status:
  filtered = [o for o in orders if o['status'] == status]
if min_total:
  filtered = [o for o in orders if o['total'] &gt;= min_total]
if max_total:
  filtered = [o for o in orders if o['total'] &lt;= max_total]
C
filtered = orders
if status:
  filtered = [o for o in filtered if o['status'] == status]
if min_total:
  filtered = [o for o in filtered if o['total'] &gt;= min_total]
if max_total:
  filtered = [o for o in filtered if o['total'] &lt;= max_total]
D
filtered = orders
if status:
  filtered = [o for o in filtered if o['status'] != status]
if min_total:
  filtered = [o for o in filtered if o['total'] &lt; min_total]
if max_total:
  filtered = [o for o in filtered if o['total'] &gt; max_total]
Attempts:
2 left
💡 Hint
Apply filters step by step on the current filtered list.