tezvyn:

⚙️Backend Dev

Backend engineering, APIs, and databases

182 bites

Python & FastAPI30 sec read

Why are contextvars better than threading.local in async Python?

This tests whether you know async tasks share OS threads, making thread-local storage unsafe for request state. A great answer notes ContextVar is task-local and resets automatically, while threading.local bleeds across concurrent coroutines.

Python & FastAPI30 sec read

How do you gracefully cancel and clean up an asyncio task?

This tests asyncio cooperative cancellation and cleanup. A strong answer covers catching CancelledError at await points, using try/finally or async context managers for cleanup, and re-raising.

Python & FastAPI30 sec read

When should you use asyncio.Lock over threading.Lock?

This tests cooperative multitasking knowledge: asyncio.Lock yields to the event loop via await, while threading.Lock blocks the OS thread and freezes the loop. A red flag is claiming threading.Lock works because locks are universal.

Python & FastAPI30 sec read

Explain the asyncio event loop and cooperative multitasking

Tests if you view the event loop as a single-threaded orchestrator, not magic parallelism. Strong answers note it runs tasks and callbacks, manages a ready queue, and yields control at await. Red flag: calling it multithreading or parallel execution.

Python & FastAPI30 sec read

How do you safely execute blocking code from an async function

Tests whether you know how to prevent event loop blocking by offloading sync work. Name asyncio.to_thread or run_in_executor, explain ThreadPoolExecutor scheduling, and note when processes beat threads.

Python & FastAPI30 sec read

Explain await's purpose, awaitable types, and event loop signaling

This tests whether you understand await as a yield point. A strong answer lists three awaitables (coroutines, Tasks, Futures), explains await yields control to the event loop until completion, and warns that a bare coroutine call does not run it.

Python & FastAPI30 sec read

What is the difference between async def and regular functions?

This tests async def call semantics. A strong answer contrasts regular functions, which execute immediately, with async def, which returns a coroutine object that does nothing until awaited or wrapped in create_task.

Python & FastAPI30 sec read

How do you apply a common path prefix across FastAPI routers?

Tests whether you know APIRouter decouples routes from path prefixes. Use relative paths in APIRouter, then mount with app.include_router(router, prefix="/api/v1"). This keeps modules reusable. Red flag: hardcoding the full absolute path in every decorator.

Python & FastAPI30 sec read

How do you include a router in your main FastAPI app?

WHAT IT TESTS: knowledge of the APIRouter wiring pattern. ANSWER OUTLINE: create APIRouter in another file, import it into main.py, then call app.include_router(router). RED FLAG: rewriting endpoints manually in main.py rather than using include_router.

Python & FastAPI30 sec read

What is APIRouter in FastAPI and why use it?

It tests your grasp of modular architecture in FastAPI. APIRouter splits routes into separate modules so you include them in the main app with prefixes, tags, and dependencies.

Python & FastAPI31 sec read

How does lifecycle differ for global vs path operation dependencies?

WHAT IT TESTS: Per-request scoping in FastAPI. ANSWER OUTLINE: Global deps run on every request to any route; path-local deps run only for that route; expensive setup belongs in a lifespan event or cached singleton, not a dependency.

Python & FastAPI30 sec read

How would you implement a dependency requiring multi-source parameters?

Tests if you know FastAPI resolves dependency params like endpoint params. Great answers annotate each parameter with its source inside the dependency so FastAPI injects them independently. Red flag: manually parsing Request or merging values in the endpoint.

Python & FastAPI30 sec read

How do you declare a function as a dependency, and why?

Tests FastAPI dependency injection basics. Answer: create a function, import Depends, and add it to path operation parameters so FastAPI injects it. Purpose: routes declare what they need instead of hard-coding shared logic.

Python & FastAPI30 sec read

How would you use a Pydantic response_model to enforce output structure?

Tests separation of internal models from API contracts. Define a Pydantic output model with only safe fields, set it as the endpoint response_model, and let FastAPI filter and validate.

Python & FastAPI30 sec read

How do you define a Pydantic model and use it in FastAPI?

WHAT IT TESTS: Whether you use Pydantic BaseModel for request body validation in FastAPI. ANSWER OUTLINE: Subclass BaseModel with name str and age int, then type-hint the parameter with the model.

Python & FastAPI30 sec read

Model an Order with a nested Product list in Pydantic

It tests Pydantic nested model composition. Define Product as BaseModel, then Order with products: list[Product]; Pydantic recursively coerces each dict and raises ValidationError on failure. A red flag is insisting on manual iteration.

Python & FastAPI30 sec read

How do you prevent password_hash from appearing in a FastAPI response?

Tests FastAPI response filtering and the security practice of separating DB schemas from API contracts. A strong answer proposes a dedicated output model omitting the field, then cites response_model_exclude. Red flag: manual dict deletion or monkey-patching.

Python & FastAPI30 sec read

Implement a custom validator for a single Pydantic model field

WHAT IT TESTS: Your grasp of Pydantic v2 field validation hooks. ANSWER OUTLINE: Use @field_validator as a classmethod, raise ValueError on failure, return the value. RED FLAG: Validating outside the model or confusing v1 @validator with v2.

Python & FastAPI30 sec read

Enforce positive price and SKU format using Pydantic Field without custom validators

WHAT IT TESTS: Pydantic V2 Field constraints vs custom validators. ANSWER OUTLINE: Use Field(gt=0) for price and Field(pattern=r'^ITEM-\d{5}$') for SKU; mention Annotated. RED FLAG: Suggesting @field_validator or conint/constr.

Python & FastAPI30 sec read

What is the difference between a Pydantic default and Optional field?

WHAT IT TESTS: Separation of type constraints from requiredness. ANSWER OUTLINE: Both forms are non-required; str = 'guest' rejects None, Optional[str] = None accepts it.

Backend Dev · Tezvyn