🐍 Python for Everyone

← Back to practice bank

Chapter 22 Coding Challenges 50-65 min, auto-graded

Coding Challenges: Building Web Apps & APIs

8 HackerRank-style challenges, auto-graded right in your browser — no setup required, and no live Flask/FastAPI server or framework install needed. Write your function in the code box, then click Run Tests to check it against a set of test cases (including edge cases). Stuck? Every challenge has a Show Solution reveal at the bottom. Only uses what Chapters 1-22 have covered so far. No import beyond json/os/re/math/datetime/random/csv/threading/concurrent.futures.

How grading works Your code runs entirely in your browser using Pyodide (Python compiled to WebAssembly) — nothing is uploaded anywhere. Keep the function name in the stub exactly as given, since the grader calls it by name.

1 Classify a Framework's Response Shape Easy

Write a function classify_response_shape(framework_name) that returns "HTML" if framework_name is "Flask", "JSON" if it's "FastAPI", and "unknown" otherwise.

Constraints: framework_name is always a string.
Examples:
classify_response_shape("Flask") → "HTML"
classify_response_shape("FastAPI") → "JSON"
challenge_1.py
Show Solution
solution_1.py
def classify_response_shape(framework_name):
    if framework_name == "Flask":
        return "HTML"
    if framework_name == "FastAPI":
        return "JSON"
    return "unknown"

2 Build a Route Map Entry Easy

Write a function build_route_entry(path, view_function_name) that returns {"path": path, "view": view_function_name} — the same pairing an @app.route(path) decorator registers with its view function.

Constraints: both arguments are strings.
Examples:
build_route_entry("/tasks", "list_tasks") → {"path": "/tasks", "view": "list_tasks"}
challenge_2.py
Show Solution
solution_2.py
def build_route_entry(path, view_function_name):
    return {"path": path, "view": view_function_name}

3 Read a Query Parameter Safely Easy

Write a function get_query_value(query_params, key, default=None) that returns query_params.get(key, default) — the same safe-lookup pattern Flask's request.args and FastAPI's query parameters both rely on.

Constraints: query_params is a dict; key is a string.
Examples:
get_query_value({"q": "python"}, "q") → "python"
get_query_value({}, "q", "none") → "none"
challenge_3.py
Show Solution
solution_3.py
def get_query_value(query_params, key, default=None):
    return query_params.get(key, default)

4 Convert a Path Parameter Medium

Write a function convert_path_value(raw_value, target_type_name) that converts the string raw_value to the type named by target_type_name (one of "int", "float", or "str") and returns the converted value — the same conversion FastAPI performs automatically on a path parameter, based on its type hint, before a route function ever runs.

Constraints: target_type_name is always one of "int", "float", "str"; raw_value is always convertible.
Examples:
convert_path_value("42", "int") → 42
convert_path_value("3.5", "float") → 3.5
challenge_4.py
Show Solution
solution_4.py
def convert_path_value(raw_value, target_type_name):
    types = {"int": int, "float": float, "str": str}
    return types[target_type_name](raw_value)

5 Filter Tasks by an Optional Query Param Medium

Write a function filter_tasks_by_done(tasks, done=None) that returns tasks unchanged if done is None, otherwise returns only the dicts in tasks whose "done" field equals done — the same behavior as a FastAPI route's optional, default-valued query parameter.

Constraints: tasks is a list of dicts, each with a "done" key.
Examples:
filter_tasks_by_done([{"done": True}, {"done": False}], True) → [{"done": True}]
challenge_5.py
Show Solution
solution_5.py
def filter_tasks_by_done(tasks, done=None):
    if done is None:
        return tasks
    return [t for t in tasks if t["done"] == done]

6 Validate a Required String Field Medium

Write a function validate_required_string_field(body, field_name) that returns True if body has a key field_name whose value is a non-empty string, and False otherwise — modeling what a Pydantic model checks automatically for a required str field.

Constraints: body is a dict; field_name is a string.
Examples:
validate_required_string_field({"title": "X"}, "title") → true
validate_required_string_field({}, "title") → false
challenge_6.py
Show Solution
solution_6.py
def validate_required_string_field(body, field_name):
    return (
        field_name in body
        and isinstance(body[field_name], str)
        and len(body[field_name]) > 0
    )

7 Apply a Model's Default Value Easy

Write a function apply_model_default(body, field_name, default_value) that returns a new dict equal to body, with default_value set for field_name only if it isn't already present — modeling how an optional Pydantic model field with a default behaves.

Constraints: body is a dict; field_name is a string.
Examples:
apply_model_default({"title": "X"}, "done", false) → {"title": "X", "done": false}
challenge_7.py
Show Solution
solution_7.py
def apply_model_default(body, field_name, default_value):
    result = dict(body)
    if field_name not in result:
        result[field_name] = default_value
    return result

8 Does This Response Need a Separate Frontend? Easy

Write a function needs_separate_frontend(response_body) that returns True if response_body is a dict or a list (JSON-shaped data, needing something else to render it), and False if it's a str (already-rendered HTML, ready to display as-is).

Constraints: response_body is a dict, list, or string.
Examples:
needs_separate_frontend({"title": "X"}) → true
needs_separate_frontend("<html></html>") → false
challenge_8.py
Show Solution
solution_8.py
def needs_separate_frontend(response_body):
    return isinstance(response_body, (dict, list))
← Practice Bank Interview Questions →