0
0
Rest APIprogramming~20 mins

Search parameter in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Search Parameter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this REST API search parameter example?

Consider a REST API endpoint that supports a search parameter to filter users by age greater than 30.

Given the following request URL:

GET /users?age_gt=30

And the server code snippet that filters users:

users = [
  {"name": "Alice", "age": 25},
  {"name": "Bob", "age": 35},
  {"name": "Charlie", "age": 40}
]

filtered = [user for user in users if user["age"] > int(request.args.get("age_gt", 0))]
print(filtered)

What will be printed?

Rest API
users = [
  {"name": "Alice", "age": 25},
  {"name": "Bob", "age": 35},
  {"name": "Charlie", "age": 40}
]

class Request:
    def __init__(self):
        self.args = {"age_gt": "30"}

request = Request()

filtered = [user for user in users if user["age"] > int(request.args.get("age_gt", 0))]
print(filtered)
A[{'name': 'Bob', 'age': 35}, {'name': 'Charlie', 'age': 40}]
B[{'name': 'Alice', 'age': 25}]
C[]
D[{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 35}, {'name': 'Charlie', 'age': 40}]
Attempts:
2 left
💡 Hint

Think about which users have age greater than 30.

🧠 Conceptual
intermediate
1:00remaining
Which HTTP method is typically used with search parameters in REST APIs?

When you want to retrieve data filtered by search parameters in a REST API, which HTTP method should you use?

APOST
BPUT
CGET
DDELETE
Attempts:
2 left
💡 Hint

Think about which method is used to get data without changing it.

🔧 Debug
advanced
2:00remaining
What error does this search parameter code raise?

Look at this code snippet that tries to filter products by a minimum price from a search parameter:

products = [
  {"name": "Pen", "price": 1.5},
  {"name": "Notebook", "price": 3.0}
]

min_price = request.args.get("min_price")
filtered = [p for p in products if p["price"] >= min_price]
print(filtered)

What error will this code raise when the request URL is /products?min_price=2?

Rest API
products = [
  {"name": "Pen", "price": 1.5},
  {"name": "Notebook", "price": 3.0}
]

class Request:
    def __init__(self):
        self.args = {"min_price": "2"}

request = Request()

min_price = request.args.get("min_price")
filtered = [p for p in products if p["price"] >= min_price]
print(filtered)
ANo error, prints all products
BSyntaxError: invalid syntax
CKeyError: 'min_price'
DTypeError: '>=' not supported between instances of 'float' and 'str'
Attempts:
2 left
💡 Hint

Check the types of p["price"] and min_price.

📝 Syntax
advanced
1:30remaining
Which option correctly extracts a search parameter 'category' from a Flask request?

In a Flask app, you want to get the 'category' search parameter from the URL query string. Which code snippet is correct?

Acategory = request.args.get('category', default='all')
Bcategory = request.get.args('category')
Ccategory = request.args['category']()
Dcategory = request.args.get['category']
Attempts:
2 left
💡 Hint

Remember the syntax for getting query parameters in Flask.

🚀 Application
expert
2:30remaining
How many items are returned by this complex search parameter filter?

Given this list of books and a search parameter 'year' filtering books published after that year:

books = [
  {"title": "Book A", "year": 1999},
  {"title": "Book B", "year": 2005},
  {"title": "Book C", "year": 2010},
  {"title": "Book D", "year": 2015}
]

search_year = int(request.args.get("year", "2000"))
filtered_books = [b for b in books if b["year"] > search_year]
print(len(filtered_books))

If the request URL is /books?year=2005, how many books will be printed?

Rest API
books = [
  {"title": "Book A", "year": 1999},
  {"title": "Book B", "year": 2005},
  {"title": "Book C", "year": 2010},
  {"title": "Book D", "year": 2015}
]

class Request:
    def __init__(self):
        self.args = {"year": "2005"}

request = Request()

search_year = int(request.args.get("year", "2000"))
filtered_books = [b for b in books if b["year"] > search_year]
print(len(filtered_books))
A4
B2
C1
D3
Attempts:
2 left
💡 Hint

Count books with year greater than 2005.