0
0
FastAPIframework~20 mins

List and set validation in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI List and Set Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when validating a list of integers with FastAPI?
Consider this FastAPI endpoint that expects a list of integers. What will be the response if the client sends the JSON body: {"numbers": [1, 2, "3", 4]}?
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List

app = FastAPI()

class NumbersModel(BaseModel):
    numbers: List[int]

@app.post("/validate")
async def validate_numbers(data: NumbersModel):
    return {"sum": sum(data.numbers)}
AReturns {"sum": 10} because "3" is automatically converted to int 3
BRaises a validation error because "3" is a string, not an int
CReturns {"sum": 7} ignoring the string "3" in the list
DRaises a runtime error during sum() because of mixed types
Attempts:
2 left
💡 Hint
Pydantic automatically coerces strings like "3" to int 3 in List[int].
📝 Syntax
intermediate
2:00remaining
Which code correctly validates a set of unique strings in FastAPI?
You want to accept a set of unique strings in a POST request body. Which of these Pydantic model definitions is correct?
A
class Model(BaseModel):
    items: Set[str]
B
class Model(BaseModel):
    items: set[str]
C
class Model(BaseModel):
    items: list[str]
D
class Model(BaseModel):
    items: Set
Attempts:
2 left
💡 Hint
Remember to import the correct type and use proper capitalization.
state_output
advanced
2:00remaining
What is the output of this FastAPI endpoint when validating a set with duplicates?
Given this endpoint and input, what will be the output? Input JSON: {"tags": ["python", "fastapi", "python"]}
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel
from typing import Set

app = FastAPI()

class TagModel(BaseModel):
    tags: Set[str]

@app.post("/tags")
async def get_tags(data: TagModel):
    return {"count": len(data.tags), "tags": sorted(data.tags)}
A{"count": 3, "tags": ["fastapi", "python", "python"]}
B{"count": 1, "tags": ["python"]}
CValidation error because input is a list, not a set
D{"count": 2, "tags": ["fastapi", "python"]}
Attempts:
2 left
💡 Hint
Think about how sets handle duplicates and how Pydantic converts lists to sets.
🔧 Debug
advanced
2:00remaining
Why does this FastAPI endpoint raise a validation error?
Examine the code and input. Why does validation fail? Input JSON: {"values": [1, 2, 3.5]} Code: from fastapi import FastAPI from pydantic import BaseModel from typing import List app = FastAPI() class ValuesModel(BaseModel): values: List[int] @app.post("/values") async def values_endpoint(data: ValuesModel): return {"sum": sum(data.values)}
ABecause List[int] does not accept any numbers
BBecause sum() cannot add floats and ints together
CBecause 3.5 is a float, not an int, causing validation to fail
DBecause the endpoint is missing async keyword
Attempts:
2 left
💡 Hint
Check the types inside the list and how Pydantic validates them.
🧠 Conceptual
expert
3:00remaining
How does Pydantic handle default values for sets in models?
Consider this Pydantic model: class MyModel(BaseModel): items: Set[str] = set() What is a potential problem with this code when used in FastAPI endpoints?
AThe code raises a syntax error because set() cannot be used as a default
BAll instances share the same default set object, causing unexpected shared state
CFastAPI rejects the model because sets cannot have default values
DPydantic automatically creates a new set for each instance, so no problem
Attempts:
2 left
💡 Hint
Think about how mutable default arguments behave in Python functions and classes.