🐍 Python for Everyone

← Back to practice bank

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

Coding Challenges: Modules & Packages

8 HackerRank-style challenges, auto-graded right in your browser — no setup required. Write your function in the code box, then click Run Tests to check it against a set of test cases (including edge cases like 0 and leap years). Stuck? Every challenge has a Show Solution reveal at the bottom. Only uses what Chapters 1-11 have covered so far — def, everything through decorators, and the math and datetime modules. No try/except, file I/O, classes, or any other import.

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.
Why no random challenges here? These challenges are graded by comparing your function's output against one fixed, exact expected value per test case. That works great for math and datetime, but random's whole point is that its output is different every time — so it's saved for the practice bank instead, where you check a property of the result instead of an exact match. Dates below are always passed in and returned as plain strings (like "2026-07-05"), since that's the only date format that survives being sent as JSON test data.

1 Hypotenuse Easy

Write a function hypotenuse(a, b) that returns the length of the hypotenuse of a right triangle with legs a and b, using math.sqrt, rounded to 2 decimal places.

Constraints: a and b are non-negative numbers; round the final result with round(..., 2).
Examples:
hypotenuse(3, 4) → 5.0
hypotenuse(5, 12) → 13.0
hypotenuse(0, 5) → 5.0
challenge_1.py
Show Solution
solution_1.py
import math


def hypotenuse(a, b):
    return round(math.sqrt(a ** 2 + b ** 2), 2)

2 Round Up to the Nearest Multiple Easy

Write a function round_up_to_multiple(n, multiple) that rounds n up to the nearest multiple of multiple, using math.ceil(n / multiple) * multiple — the same "how many boxes do I need" pattern from a warehouse packing problem.

Constraints: n may be 0 or already an exact multiple (in which case it stays the same).
Examples:
round_up_to_multiple(23, 5) → 25
round_up_to_multiple(20, 5) → 20
round_up_to_multiple(0, 5) → 0
challenge_2.py
Show Solution
solution_2.py
import math


def round_up_to_multiple(n, multiple):
    return math.ceil(n / multiple) * multiple

3 Circle Area Easy

Write a function circle_area(radius) that returns the area of a circle (math.pi * radius ** 2), rounded to 2 decimal places.

Constraints: radius may be 0; round the final result with round(..., 2).
Examples:
circle_area(1) → 3.14
circle_area(2) → 12.57
circle_area(0) → 0.0
challenge_3.py
Show Solution
solution_3.py
import math


def circle_area(radius):
    return round(math.pi * radius ** 2, 2)

4 Rounded Power Easy

Write a function power_rounded(base, exponent) that returns base raised to exponent using math.pow, rounded to 2 decimal places.

Constraints: exponent may be 0 (any base to the 0th power is 1.0); base/exponent may be a float.
Examples:
power_rounded(2, 10) → 1024.0
power_rounded(2, 0) → 1.0
power_rounded(1.5, 2) → 2.25
challenge_4.py
Show Solution
solution_4.py
import math


def power_rounded(base, exponent):
    return round(math.pow(base, exponent), 2)

5 Days Between Two Dates Medium

Write a function days_between_iso(date_a, date_b) that takes two ISO-formatted date strings (like "2026-07-05"), converts each to a datetime.date with datetime.date.fromisoformat(...), and returns the number of days from date_a to date_b as an integer (using date subtraction and .days). Dates are passed and returned as plain strings so the test cases can be written as ordinary JSON.

Constraints: date_b may come before date_a (return a negative number); the two dates may be equal (return 0); test cases include a leap year.
Examples:
days_between_iso("2026-01-01", "2026-07-05") → 185
days_between_iso("2026-01-01", "2026-01-01") → 0
days_between_iso("2026-07-05", "2026-01-01") → -185
challenge_5.py
Show Solution
solution_5.py
import datetime


def days_between_iso(date_a, date_b):
    a = datetime.date.fromisoformat(date_a)
    b = datetime.date.fromisoformat(date_b)
    return (b - a).days

6 Day of the Week Medium

Write a function day_of_week_name(year, month, day) that builds a datetime.date(year, month, day) and returns its weekday name as a string (like "Sunday"), using .strftime("%A"). The date is passed in as three separate integers (year, month, day) since that's a JSON-safe way to represent a date without needing a special date type in the test data.

Constraints: year/month/day always form a valid calendar date.
Examples:
day_of_week_name(2026, 7, 5) → "Sunday"
day_of_week_name(2026, 1, 1) → "Thursday"
day_of_week_name(2000, 1, 1) → "Saturday"
challenge_6.py
Show Solution
solution_6.py
import datetime


def day_of_week_name(year, month, day):
    d = datetime.date(year, month, day)
    return d.strftime("%A")

7 Add Days to a Date Medium

Write a function add_days_iso(date_str, days) that takes an ISO date string and an integer number of days, and returns a new ISO date string that many days later (or earlier, if days is negative). Convert with datetime.date.fromisoformat(...), add a datetime.timedelta(days=days), then convert back to a string with .isoformat() so the result stays JSON-safe.

Constraints: days may be 0 or negative; results may cross a month or year boundary.
Examples:
add_days_iso("2026-01-01", 30) → "2026-01-31"
add_days_iso("2026-01-01", 0) → "2026-01-01"
add_days_iso("2026-01-01", 365) → "2027-01-01"
challenge_7.py
Show Solution
solution_7.py
import datetime


def add_days_iso(date_str, days):
    d = datetime.date.fromisoformat(date_str)
    result = d + datetime.timedelta(days=days)
    return result.isoformat()

8 Age in Full Years Medium

Write a function age_in_years(birth_date_str, on_date_str) that takes two ISO date strings and returns how many full years old someone born on birth_date_str would be on on_date_str — the same logic a real signup form uses to check a minimum age. Compute the plain year difference first, then subtract 1 if the birthday hasn't happened yet that year (compare (month, day) tuples).

Constraints: on_date_str is always on or after birth_date_str; test cases include a birthday that hasn't happened yet in the target year, and a Feb 29 birth date.
Examples:
age_in_years("2000-07-05", "2026-07-05") → 26
age_in_years("2000-07-06", "2026-07-05") → 25
age_in_years("2000-01-01", "2026-12-31") → 26
challenge_8.py
Show Solution
solution_8.py
import datetime


def age_in_years(birth_date_str, on_date_str):
    birth = datetime.date.fromisoformat(birth_date_str)
    on_date = datetime.date.fromisoformat(on_date_str)
    years = on_date.year - birth.year
    if (on_date.month, on_date.day) < (birth.month, birth.day):
        years -= 1
    return years
← Practice Bank Interview Questions →