Skip to content
tezvyn:

Python & FastAPI

Python, Django, FastAPI, Flask, async Python

244 bites

Test yourself: Top 30 Python & FastAPI interview questionsMultiple choice, with the correct answer and why it is correct on every question. Free, no sign-in.

Everything in Python & FastAPI, page 2

intermediate1 min read

asyncio.gather vs asyncio.wait

Gather returns ordered results and propagates the first exception (or captures them); wait returns done/pending sets and never raises, you inspect each.

intermediate1 min read

How FastAPI uses type hints for validation

Hints drive parsing, validation, and conversion; a path declared int is coerced or returns 422; OpenAPI is auto-generated.

intermediate2 min read

Explain multi-stage Docker builds for Python and builder vs runtime

Tests separation of build-time and runtime concerns. A strong answer contrasts the builder stage (gcc, headers, wheels) with the runtime stage (slim base, copied artifacts, no compiler). Red flag: citing size alone while ignoring security and caching.

intermediate2 min read

How do you manage configuration and secrets for a containerized FastAPI app?

Tests 12-factor config separation and Docker secret hygiene. A strong answer uses pydantic-settings with runtime env vars, lru_cache, and keeps .env out of the image. Red flag: baking credentials into Dockerfile layers or committing .env files.

What are the key responsibilities for Nginx versus Uvicorn?
intermediate2 min read

What are the key responsibilities for Nginx versus Uvicorn?

This tests the reverse-proxy versus ASGI-server boundary. A strong answer gives Nginx TLS termination, static files, buffering, and load balancing, while Uvicorn runs the Python app and async workers.

easy2 min read

Why use an ASGI server like Uvicorn instead of the dev server?

Tests: dev vs prod environment distinction. Answer: Uvicorn is a prod server program; contrast dev server's constant restart/break/fix cycle with prod needs for performance, stability, and uninterrupted access.

Walk me through a basic Dockerfile for a FastAPI app
easy2 min read

Walk me through a basic Dockerfile for a FastAPI app

Slim base, install deps before app code to cache layers, expose port, exec-form CMD for Uvicorn.

How do you inspect WebSocket close codes in FastAPI?
advanced2 min read

How do you inspect WebSocket close codes in FastAPI?

Tests WebSocket lifecycle handling in FastAPI. Strong answers catch the disconnect exception, read its code attribute, and log 1000 for normal closures versus 1001/1006 for crashes.

advanced2 min read

How do you handle slow startup without blocking the FastAPI event loop?

It tests FastAPI lifespan events and event loop hygiene. Use an async lifespan to offload blocking model loading to a thread pool, track readiness with a global flag, and return 503 for early requests. Never block the event loop in startup handlers.

How do you broadcast WebSocket messages to all clients across server nodes?
advanced2 min read

How do you broadcast WebSocket messages to all clients across server nodes?

Tests WebSocket horizontal scaling and pub/sub backplanes. A strong answer names a broker like Redis, describes cross-node fan-out, and keeps connection state purely local.

How do you authenticate a FastAPI WebSocket connection?
intermediate2 min read

How do you authenticate a FastAPI WebSocket connection?

This tests WebSocket limits and FastAPI dependency injection. Pass the JWT via query parameter or cookie at handshake, validate it with Depends, and reject with HTTP 403 or 1008 close.

intermediate2 min read

Use startup events to initialize a database pool and inject it

This tests FastAPI lifespan hooks and dependency injection for shared state. A strong answer creates the pool in an async startup handler, stores it on app.state, and accesses it via a dependency in routes. A red flag is creating a fresh pool per request.

How do you handle a WebSocket client disconnect in FastAPI?
intermediate2 min read

How do you handle a WebSocket client disconnect in FastAPI?

Cite installing websockets, Handling disconnections and multiple clients pattern, and Depends.

easy2 min read

What are startup and shutdown events in FastAPI?

Tests app lifespan hooks and resource lifecycle. Startup creates DB pools before traffic arrives; shutdown closes them after the last request. These decorators are deprecated; prefer lifespan context managers. Red flag: per-request middleware.

How do you define a WebSocket endpoint in FastAPI?
easy2 min read

How do you define a WebSocket endpoint in FastAPI?

Import WebSocket, use @app.websocket, await accept, receive_text, then send_text.

advanced2 min read

How do you directly modify FastAPI's generated OpenAPI dictionary?

Tests deep FastAPI lifecycle knowledge. Override app.openapi: save the original, call it to get the dict, mutate it, cache on app.openapi_schema, and return. Red flag: rewriting /openapi.json in middleware or touching the schema cache directly.

How do you disable FastAPI docs but keep the OpenAPI schema?
advanced2 min read

How do you disable FastAPI docs but keep the OpenAPI schema?

Tests FastAPI constructor routing: docs_url, redoc_url, and openapi_url. Answer: pass docs_url=None and redoc_url=None while keeping openapi_url="/openapi.json", gated by env var. Red flag: middleware or manual route deletion instead of native configuration.

Add a custom x- field to a FastAPI path operation schema
advanced2 min read

Add a custom x- field to a FastAPI path operation schema

Tests knowledge of FastAPI's built-in OpenAPI extension hook. Answer: cite the path operation decorator's extra-schema dict (OpenAPI Extra) to merge x-internal-id directly. Red flag: proposing manual JSON editing or schema post-processing.

How do you mark a FastAPI endpoint as deprecated?
intermediate2 min read

How do you mark a FastAPI endpoint as deprecated?

This tests decorator-level OpenAPI configuration in FastAPI. Pass deprecated=True to the path operation decorator, e.g. @app.get("/old", deprecated=True), so Swagger UI shows a strikethrough. Red flag: burying a deprecation warning in the docstring instead.

How do you group FastAPI endpoints under tags and describe groups?
intermediate2 min read

How do you group FastAPI endpoints under tags and describe groups?

Tests FastAPI's two-step tag system: decorators label routes, and openapi_tags supplies group descriptions. Good answers cover tagging paths with tags=["users"], then defining metadata in FastAPI(openapi_tags=[...]) with matching names.

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles