0
0
MLOpsdevops~20 mins

REST API serving with FastAPI in MLOps - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this FastAPI endpoint response?
Given this FastAPI endpoint code, what will be the JSON response when a GET request is made to /items/42?
MLOps
from fastapi import FastAPI
app = FastAPI()

@app.get('/items/{item_id}')
async def read_item(item_id: int):
    return {"item_id": item_id, "name": f"Item {item_id}"}
A{"item_id": 42, "name": "item 42"}
B{"item_id": "42", "name": "Item 42"}
C{"item_id": 42, "name": "Item 42"}
D{"item_id": 42}
Attempts:
2 left
💡 Hint
Remember that FastAPI converts path parameters to the declared type.
Configuration
intermediate
1:30remaining
Which uvicorn command correctly runs the FastAPI app from main.py with auto-reload?
You have a FastAPI app in main.py with the app instance named app. Which command will start the server with auto-reload enabled?
Auvicorn main.py:app --reload
Buvicorn app:main --reload
Cuvicorn main.app --reload
Duvicorn main:app --reload
Attempts:
2 left
💡 Hint
The syntax is uvicorn module_name:app_instance.
🔀 Workflow
advanced
2:00remaining
What is the correct sequence to deploy a FastAPI app with Docker?
Arrange these steps in the correct order to build and run a FastAPI app using Docker.
A1,2,3,4
B2,1,3,4
C1,3,2,4
D3,2,1,4
Attempts:
2 left
💡 Hint
Think about writing the Dockerfile before building the image.
Troubleshoot
advanced
1:30remaining
Why does this FastAPI app raise a 422 error on POST?
This FastAPI endpoint expects JSON with name (string) and age (int). The client sends {"name": "Alice", "age": "25"}. Why does the server respond with status 422?
MLOps
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class User(BaseModel):
    name: str
    age: int

@app.post('/users')
async def create_user(user: User):
    return user
ABecause age is sent as a string, not an integer
BBecause name is missing in the JSON
CBecause the endpoint only accepts GET requests
DBecause the JSON is missing required fields
Attempts:
2 left
💡 Hint
Check the data types expected by Pydantic models.
Best Practice
expert
2:00remaining
Which approach best improves FastAPI app performance under high load?
You want to improve the performance of a FastAPI app serving ML model predictions under heavy traffic. Which option is the best practice?
AUse a single uvicorn worker to avoid race conditions
BUse Gunicorn with multiple workers and uvicorn workers to handle concurrency
CDisable async features to simplify code
DRun uvicorn with --reload for faster code changes in production
Attempts:
2 left
💡 Hint
Consider how to handle many requests efficiently in production.