Python Backend Interview Questions
The Python backend questions interviewers ask most in FastAPI, Django, and Flask roles — covering async, ORMs, API design, task queues, and testing — each with a clear answer. Then rehearse them in a live mock.
11 common Python Backend questions
What is the difference between Django, Flask, and FastAPI?
Django is a batteries-included framework with ORM, admin, auth, and templating — ideal for large apps. Flask is a micro-framework giving you full control with minimal defaults — good for small services. FastAPI is modern and async-first, built on Starlette and Pydantic, with automatic OpenAPI docs — the best choice for high-performance APIs. Choose based on scale, team preference, and async needs.
How does Python's async/await work and when should you use it?
async functions return coroutines that run in an event loop. await suspends the coroutine until the awaited task (usually I/O) completes, letting other coroutines run. Use async for I/O-bound work — HTTP calls, DB queries, file reads — where you want to handle many concurrent requests without threads. For CPU-bound work, use multiprocessing instead; async won't help there.
What are Pydantic models and why are they useful in FastAPI?
Pydantic models define data shapes with Python type hints and validate automatically at parse time — raising clear errors for invalid input. FastAPI uses them for request body parsing, query parameters, and response serialisation, giving you automatic validation, IDE autocompletion, and generated OpenAPI docs for free.
What is an ORM and how does Django ORM compare to SQLAlchemy?
An ORM maps Python classes to database tables so you write Python instead of SQL. Django ORM is tightly coupled to Django, simple to use, and handles migrations via makemigrations. SQLAlchemy is standalone, more powerful, and gives fine-grained control — used widely with Flask and FastAPI. For complex queries or multiple database support, SQLAlchemy is more flexible; for standard CRUD in a Django app, the built-in ORM is faster to work with.
What is Celery and when would you use it?
Celery is a distributed task queue for running work asynchronously or on a schedule, backed by a broker like Redis or RabbitMQ. Use it for tasks that are slow or can be deferred — sending emails, processing images, generating reports, or retrying failed operations — so the HTTP response isn't blocked.
How do you handle database migrations in Python backend projects?
In Django, run makemigrations to generate migration files from model changes, then migrate to apply them. In SQLAlchemy-based projects use Alembic — autogenerate detects schema diffs or you write them manually. Always review auto-generated migrations, never edit them after merging, and run migrations in CI before deploying.
Explain REST vs GraphQL — when would you choose each?
REST uses fixed endpoints per resource (GET /users/{id}) and is simple, cacheable, and widely understood. GraphQL uses a single endpoint where clients specify exactly what fields they need — reducing over/under-fetching for complex, nested data. Choose REST for simple CRUD APIs and public APIs; choose GraphQL when clients have varying data needs or you're building a BFF layer.
How do you secure a Python API? Walk through the main concerns.
Authentication (JWT or OAuth2 with libraries like python-jose or Authlib), authorisation (check permissions per resource), input validation (Pydantic or marshmallow), SQL injection prevention (use ORM or parameterised queries, never string-format SQL), rate limiting (slowapi or gateway-level), HTTPS everywhere, secrets in env vars (never in code), CORS configured narrowly, and dependency scanning in CI.
What is the GIL and how does it affect backend Python?
The Global Interpreter Lock allows only one thread to execute Python bytecode at a time, so threads can't parallelise CPU-bound work. For I/O-bound concurrency use async or threads (GIL releases during I/O). For CPU-bound parallelism use multiprocessing or offload to C extensions. Gunicorn/uvicorn workers side-step this by running multiple processes.
How do you write and organise tests in a Python backend project?
Use pytest as the test runner. Organise by type: unit tests (test pure functions with mocks for dependencies), integration tests (test DB layer with a test DB or transactions rolled back after each test), and API tests (use FastAPI's TestClient or Django's APIClient). Mark slow tests, run fast ones in CI on every push, slow ones nightly. Use fixtures for DRY setup and conftest.py for shared helpers.
What is dependency injection in FastAPI?
FastAPI's Depends() system lets you declare dependencies (DB sessions, auth checks, config, shared clients) as functions that FastAPI resolves and injects into route handlers. This keeps handlers thin, makes dependencies testable by overriding them in tests, and handles setup/teardown (e.g. opening and closing a DB session) through generator-based dependencies with yield.
Ready to practice out loud?
Reading answers is one thing — saying them under pressure is another. Run a free AI mock interview and get scored feedback.
Start a mock interview