0
0
FastAPIframework~20 mins

Bulk operations in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bulk Operations Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Bulk Create Endpoint Behavior

Consider a FastAPI endpoint designed to create multiple user records at once using a list of user data. What will be the response if one of the user entries in the list is missing a required field?

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

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

@app.post('/users/bulk')
async def create_users(users: list[User]):
    return {'created': len(users)}
AThe endpoint creates all users except the one missing the field and returns the count of created users.
BThe endpoint returns a 422 Unprocessable Entity error and no users are created.
CThe endpoint ignores validation and creates all users, filling missing fields with null.
DThe endpoint returns a 500 Internal Server Error due to missing data.
Attempts:
2 left
💡 Hint

Think about how Pydantic validates input data before the endpoint logic runs.

📝 Syntax
intermediate
2:00remaining
Correct Bulk Update Syntax

Which of the following FastAPI endpoint definitions correctly accepts a list of items for bulk update using Pydantic models?

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    id: int
    value: str
A
@app.put('/items/bulk')
async def update_items(items: list[Item]):
    return {'updated': len(items)}
B
@app.put('/items/bulk')
async def update_items(items: Item):
    return {'updated': 1}
C
@app.put('/items/bulk')
async def update_items(items: dict[int, Item]):
    return {'updated': len(items)}
D
@app.put('/items/bulk')
async def update_items(items: Item[]):
    return {'updated': len(items)}
Attempts:
2 left
💡 Hint

Remember how Python type hints express lists and arrays.

state_output
advanced
2:00remaining
Bulk Delete Endpoint Output

Given a FastAPI endpoint that deletes multiple records by their IDs, what will be the output if some IDs do not exist in the database?

FastAPI
from fastapi import FastAPI

app = FastAPI()

fake_db = {1: 'a', 2: 'b', 3: 'c'}

@app.delete('/items/bulk')
async def delete_items(ids: list[int]):
    deleted = 0
    for id_ in ids:
        if id_ in fake_db:
            del fake_db[id_]
            deleted += 1
    return {'deleted': deleted, 'requested': len(ids)}
A{'deleted': number_of_existing_ids, 'requested': total_ids_passed}
B{'deleted': total_ids_passed, 'requested': total_ids_passed}
CRaises a KeyError when a non-existing ID is deleted.
D{'deleted': 0, 'requested': total_ids_passed}
Attempts:
2 left
💡 Hint

Look at how the code checks if an ID exists before deleting.

🔧 Debug
advanced
2:00remaining
Debugging Bulk Insert with Async DB

Why does this FastAPI bulk insert endpoint fail to insert all items into the database?

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Record(BaseModel):
    id: int
    data: str

fake_db = {}

@app.post('/records/bulk')
async def bulk_insert(records: list[Record]):
    for record in records:
        fake_db[record.id] = record.data
    return {'inserted': len(records)}
AThe endpoint fails because the fake_db dictionary is not thread-safe for async operations.
BThe endpoint fails because the records parameter should be a dict, not a list.
CThe endpoint fails because the loop should use await when inserting each record.
DThe endpoint works correctly; no failure occurs.
Attempts:
2 left
💡 Hint

Consider that fake_db is a simple dictionary and the code is synchronous inside the async function.

🧠 Conceptual
expert
3:00remaining
Best Practice for Bulk Operations in FastAPI

Which approach is best to handle large bulk operations in FastAPI to avoid blocking the server and ensure responsiveness?

AProcess all items synchronously in the request handler, returning only after completion.
BSplit the bulk list into single-item requests and process them one by one synchronously.
CUse background tasks or external workers to process bulk operations asynchronously after responding.
DReject bulk requests and force clients to send single-item requests.
Attempts:
2 left
💡 Hint

Think about server load and user experience when processing large data.