Interview Questions: Building Web Apps & APIs
Web frameworks sit behind nearly every job description mentioning "backend" or "full-stack," and Flask/FastAPI specifically come up constantly in Python interviews. This set covers Flask's route/render_template/request model, FastAPI's path/query params and Pydantic validation, automatic /docs generation, the Flask-vs-FastAPI tradeoff, and where a frontend fits relative to a JSON API.
1 What does a web framework actually do, and how is that different from what Chapter 19's requests library did?
A web framework (Flask, FastAPI) is the SERVER side of an HTTP exchange: it listens for incoming requests and lets your code decide what response to send back. Chapter 19's requests library was the CLIENT side: it sent requests out to some other server and read back the response. They're two ends of the exact same request/response cycle — this chapter builds the end that Chapter 19 was always talking to.
Confusing "a web framework" with "a library for making HTTP requests" — they solve opposite sides of the same problem.
Follow-up"Could the same program both serve requests with Flask/FastAPI AND make outgoing requests with the requests library?" (Yes — a real backend commonly does both: it serves its own API to clients, while also calling OTHER services' APIs internally, e.g. a payment processor or a third-party data source.)
2 Walk through what happens when a request hits a Flask route decorated with @app.route("/tasks").
Flask matches the incoming request's path (/tasks) against every registered route, finds the view function @app.route("/tasks") was placed above, and calls that function. Whatever the function returns becomes the response: a string becomes a plain response body, a dict is automatically converted to JSON, and render_template(...)'s return value is the rendered HTML page. If no route matches the path at all, Flask automatically responds with a 404.
Thinking @app.route() "runs" the function immediately at startup, rather than registering it to run later when a matching request actually arrives.
"What HTTP method does a route respond to by default, if methods= isn't specified?" (GET only — a route needs methods=["POST"] (or others) explicitly to accept anything besides GET requests.)
3 What does render_template() do, and what problem is it solving?
render_template(file, **context) loads an HTML file from a templates/ folder and fills in placeholders (Jinja2 syntax like {{ variable }} and {% for x in y %}) using the keyword arguments passed as context. It solves the same problem an f-string solves — combining a static structure with dynamic values — but for a whole HTML file loaded from disk instead of one string built in memory, which keeps HTML markup out of Python source code entirely.
Describing render_template() as executing arbitrary Python inside the HTML file, rather than filling in a template's placeholders with already-computed Python values.
"Does a Flask route always have to return HTML from render_template()?" (No — a route can return a plain string, a dict (auto-converted to JSON), or a tuple with a status code; render_template() is simply the common choice when the response genuinely is a full displayable page.)
4 What's the difference between request.args, request.form, and request.json in Flask?
All three read incoming request data and behave like a dict with .get(key, default), but from different sources: request.args reads the URL's query string (?key=value); request.form reads data submitted from an HTML <form> (a POST body, form-encoded); request.json reads a POST body when the client sent JSON specifically (the shape an API client, not a browser form, typically sends). Using the wrong one for a given request results in reading None/empty data even though the client genuinely sent something.
Treating all three as interchangeable, or not knowing which one a plain HTML form submission actually populates (it's request.form, not request.json).
"If a JavaScript frontend sends a POST request with a JSON body, which of the three would read it correctly?" (request.json — request.form is specifically for form-encoded data, which a JSON-sending client isn't using.)
5 What is a path parameter in FastAPI, and how does the type hint on it actually get used?
A path parameter is a placeholder written in curly braces inside a route's path string, like {task_id} in @app.get("/tasks/{task_id}"). Its matching function argument's type hint (task_id: int) isn't just documentation — FastAPI actually converts the raw URL string to that type before the function runs, and if the conversion fails, responds with a 422 error automatically, without the route function ever executing.
Describing the type hint as "just a comment for readability," missing that it drives real, enforced behavior in FastAPI specifically.
Follow-up"What arrives at /tasks/{task_id} in the raw URL — a string or an int?" (Always a string — URLs are text. FastAPI's type hint is what converts that string to the actual Python type the function expects.)
6 How does FastAPI distinguish a path parameter from a query parameter?
A path parameter is explicitly written inside the route's path string in curly braces; anything else in the function's signature that isn't part of the path is treated as a query parameter automatically — and if it has a default value, it becomes optional. So def list_tasks(done: bool | None = None) on a route @app.get("/tasks") (no {done} in the path) makes done a query parameter, read from ?done=true in the URL, defaulting to None if the client doesn't include it.
Thinking query parameters need a special decorator or explicit marker in FastAPI — the distinction is inferred from the function signature and the route's path string, not a separate syntax.
Follow-up"What happens to a query parameter with no default value at all?" (It becomes required — the client must include it in the query string, or FastAPI responds with a 422 error, similar to a missing required field on a Pydantic model.)
7 What is a Pydantic BaseModel, and what two things does it drive automatically in FastAPI?
A Pydantic BaseModel is a class describing expected data shape as type-hinted fields (e.g. title: str, done: bool = False). Passing one as a route function's argument makes FastAPI do two things automatically from that single class definition: (1) validate the incoming JSON body against it, rejecting anything that doesn't match with a 422 error before the function runs, and (2) include that model's shape in the automatically generated /docs page, so API consumers can see exactly what a request should look like.
Describing a Pydantic model as "just a nicer dict" without recognizing it's the single source both validation and documentation are generated from.
Follow-up"If a required field is missing from a POST request's JSON body, does the route function's code ever run?" (No — FastAPI validates against the Pydantic model BEFORE calling the route function; a failed validation returns a 422 error and the function body never executes at all.)
8 What is FastAPI's /docs page, and where does its content come from?
/docs is an automatically generated, interactive Swagger UI page listing every route, its parameters, and any Pydantic models involved, with a "Try it out" button that sends real requests from inside the browser. Nothing about it is hand-written: it's built directly from the route decorators, function signatures, type hints, and Pydantic models already present in the code, which is why it can never silently drift out of sync with the actual API the way separately maintained documentation can.
Believing /docs requires a separate documentation-writing step, or not knowing it's interactive (can actually send test requests), not just a static reference page.
"If a route's Pydantic model changes, does /docs need to be updated separately?" (No — /docs is generated fresh from the current code every time the app runs, so it automatically reflects whatever the model and route signatures currently say, with zero manual synchronization needed.)
9 Why do type hints "matter specifically" in FastAPI, compared to how they're typically used elsewhere in Python?
In most Python code (and in Flask), a type hint is optional documentation for humans and tools like linters — it doesn't change what the code actually does at runtime. In FastAPI, type hints are load-bearing: they drive real request-time validation (converting/rejecting path and query parameters, validating Pydantic model bodies) and the content of the automatically generated /docs page. The exact same syntax that's "just a hint" in one context is the actual validation mechanism in the other.
Claiming type hints "do the same thing" in every Python context — the syntax is identical, but FastAPI is specifically built to read and act on it, unlike plain Python or Flask.
Follow-up"Would writing `def get_task(task_id)` without a type hint still work in FastAPI?" (Yes, it would run — but task_id would arrive as a plain string with no automatic conversion or validation, and FastAPI's /docs page would show it as an untyped parameter, losing the exact benefit type hints provide here.)
10 When would a team choose Flask over FastAPI, or vice versa?
Flask fits a project that's mainly a server-rendered website — full HTML pages a browser displays directly, with a simpler mental model and Flask's large, mature ecosystem of extensions. FastAPI fits a project that's mainly a JSON API meant to be consumed by something else (a separate frontend, a mobile app, another service) — especially when automatic request validation and always-current interactive docs matter, or when async support is a priority. Many real systems even use both: an internal Flask-rendered admin tool alongside a FastAPI-served public API.
Red flagsClaiming one framework is universally superior rather than recognizing they're built around genuinely different default outputs.
Follow-up"Could a Flask app be gradually migrated to also expose a JSON API, without rewriting everything in FastAPI?" (Yes — Flask routes can return dicts directly, which are auto-converted to JSON, so a Flask app can serve JSON endpoints alongside its HTML-rendering routes; teams sometimes do this incrementally rather than adopting FastAPI outright.)
11 Why is a FastAPI JSON API described as "deliberately incomplete" as something a person looks at?
A FastAPI route typically returns raw JSON data, with no visual layout, styling, or interactivity attached at all — it's designed to be consumed programmatically by something else that turns that data into an actual screen: a web frontend (like a React application), a native mobile app, or another script. This isn't a limitation to work around; it's the intentional design, since the same JSON API can then serve multiple different consumers (web, mobile, internal tooling) from one backend, rather than being tied to one specific way of being displayed.
Red flagsTreating "the API doesn't render a page" as a bug or an oversight, rather than the deliberate architectural choice that makes multi-client reuse possible.
Follow-up"If a company's JSON API needs to serve both a website and a mobile app, does it need two separate backends?" (No — that's exactly the point: one JSON API can serve both, since each client (web frontend, mobile app) independently turns the same JSON response into whatever screen makes sense on that platform.)
12 Where would something like React fit relative to the Flask and FastAPI apps built in this chapter? (Conceptual only.)
React is a JavaScript library that runs in the user's browser and renders what a user actually sees and interacts with. It would make HTTP requests to a JSON API like the FastAPI app built in this chapter — the exact same kind of request Chapter 19's requests.get() made from Python — and use the JSON response to decide what to display on the screen. It would NOT typically sit in front of a Flask app rendering full HTML pages directly, since Flask's server-rendered approach already produces the complete page a browser displays, with no separate rendering step needed.
Confusing React with a backend technology, or believing this course teaches React syntax (it doesn't — this is explicitly conceptual, to show where a JSON API fits into a bigger picture).
Follow-up"Could a single project use BOTH Flask (for one part) and a React+FastAPI pair (for another)?" (Yes — real systems sometimes do exactly this, e.g. Flask for an internal admin tool rendered as plain HTML, and a separate FastAPI-backed React application for the customer-facing product, chosen per use case rather than picking one architecture company-wide.)
Rapid-Fire Review
Quick recall check — fill in each blank and hit Check.