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.
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.
framework_name is always a string.
classify_response_shape("Flask") → "HTML"classify_response_shape("FastAPI") → "JSON"
Show Solution
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.
build_route_entry("/tasks", "list_tasks") → {"path": "/tasks", "view": "list_tasks"}
Show Solution
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.
query_params is a dict; key is a string.
get_query_value({"q": "python"}, "q") → "python"get_query_value({}, "q", "none") → "none"
Show Solution
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.
target_type_name is always one of "int", "float", "str"; raw_value is always convertible.
convert_path_value("42", "int") → 42convert_path_value("3.5", "float") → 3.5
Show Solution
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.
tasks is a list of dicts, each with a "done" key.
filter_tasks_by_done([{"done": True}, {"done": False}], True) → [{"done": True}]
Show Solution
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.
body is a dict; field_name is a string.
validate_required_string_field({"title": "X"}, "title") → truevalidate_required_string_field({}, "title") → false
Show Solution
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.
body is a dict; field_name is a string.
apply_model_default({"title": "X"}, "done", false) → {"title": "X", "done": false}
Show Solution
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).
response_body is a dict, list, or string.
needs_separate_frontend({"title": "X"}) → trueneeds_separate_frontend("<html></html>") → false
Show Solution
def needs_separate_frontend(response_body):
return isinstance(response_body, (dict, list))