Coding Challenges: Testing Your Code
8 HackerRank-style challenges, auto-graded right in your browser — no local install needed, standard library only. 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-27 have covered so far.
unittest/pytest test files
lives in the practice bank and lesson, since this grader can only check
a function's return value, not a test file's structure.
1 Count Failed Tests Easy
Write a function count_failures(results_list) that takes a list of booleans representing pass (True) / fail (False) results from a test run, and returns how many of them are False.
results_list is a list of booleans, possibly empty.
count_failures([True, True, False]) → 1count_failures([]) → 0
Show Solution
def count_failures(results_list):
return sum(1 for r in results_list if r is False)
2 Would the Test Catch It? Easy
Write a function test_would_fail(actual, expected) that simulates the core of an assertEqual check: return True if actual does not equal expected (the test would fail and catch the problem), else return False.
actual and expected can be numbers, strings, or lists.
test_would_fail(5, 5) → Falsetest_would_fail(5, 6) → True
Show Solution
def test_would_fail(actual, expected):
return actual != expected
3 Valid pytest Test File Name Easy
Write a function is_valid_pytest_filename(name) that returns True if pytest's default discovery would pick up this filename as a test file. pytest's default rule: the file must end in .py, and its name (without the .py) must either start with test_ or end with _test. Matching is case-sensitive.
name is a non-empty string.
is_valid_pytest_filename("test_math.py") → Trueis_valid_pytest_filename("math.py") → False
Show Solution
def is_valid_pytest_filename(name):
if not name.endswith(".py"):
return False
stem = name[:-3]
return stem.startswith("test_") or stem.endswith("_test")
4 Catch the Off-By-One Medium
A teammate's is_last_index(i, length) helper has an off-by-one bug: it checks i == length instead of the correct i == length - 1. Write a function exposes_off_by_one_bug(n) that, for a fixed length of 5, builds BOTH the buggy version and the correct version internally, calls each with n, and returns True if the two versions disagree on this input (meaning a test using n as its input would have caught the bug), else False.
n is a non-negative int. length is always 5 inside your function.
exposes_off_by_one_bug(4) → True (the real last index; buggy version wrongly says False)exposes_off_by_one_bug(3) → False (both versions agree)
Show Solution
def exposes_off_by_one_bug(n):
def buggy_is_last_index(i, length):
return i == length # bug: should be length - 1
def correct_is_last_index(i, length):
return i == length - 1
length = 5
return buggy_is_last_index(n, length) != correct_is_last_index(n, length)
5 Simulated Retry Call Count Medium
A common thing to verify with a mock is "how many times did we retry a flaky call before it succeeded (or gave up)?" Write a function call_count_after_retries(max_retries, fails_before_success) that simulates this: starting from attempt 1, an attempt succeeds once the attempt number is strictly greater than fails_before_success; stop retrying once you succeed OR once you've made max_retries attempts, whichever comes first. Return the total number of attempts actually made.
max_retries is a positive int; fails_before_success is a non-negative int.
call_count_after_retries(5, 2) → 3 (fails on attempts 1 and 2, succeeds on attempt 3)call_count_after_retries(3, 10) → 3 (gives up after 3 attempts, never succeeds)
Show Solution
def call_count_after_retries(max_retries, fails_before_success):
attempts = 0
succeeded = False
while attempts < max_retries and not succeeded:
attempts += 1
if attempts > fails_before_success:
succeeded = True
return attempts
6 Edge Cases for a Range Check Medium
Write a function edge_case_results(low, high) that tests an inclusive range check (low <= value <= high) at its 4 classic boundary inputs, in this exact order: one below low, exactly low, exactly high, and one above high. Return a list of the 4 boolean results, in that order.
low and high are ints with low <= high.
edge_case_results(0, 10) → [False, True, True, False]
Show Solution
def in_range_inclusive(value, low, high):
return low <= value <= high
def edge_case_results(low, high):
return [
in_range_inclusive(low - 1, low, high),
in_range_inclusive(low, low, high),
in_range_inclusive(high, low, high),
in_range_inclusive(high + 1, low, high),
]
7 Fresh Fixture State Medium
Write a function fixture_values_are_isolated(num_tests) that simulates a pytest-style fixture: define an inner make_fixture() that returns a fresh empty list each call. Then, for num_tests simulated tests, call make_fixture() fresh each time, append one item to it, and record its length. Return True if every recorded length is exactly 1 (proving no state leaked between simulated tests), else False.
num_tests is a non-negative int.
fixture_values_are_isolated(3) → Truefixture_values_are_isolated(0) → True (vacuously, nothing to fail)
Show Solution
def fixture_values_are_isolated(num_tests):
def make_fixture():
return []
results_per_test = []
for i in range(num_tests):
state = make_fixture()
state.append(i)
results_per_test.append(len(state))
return all(r == 1 for r in results_per_test)
8 Descriptive Test Name Check Medium
Write a function is_descriptive_test_name(name) that flags whether a test function's name follows good naming practice. Rules: it must start with test_; what follows must not be empty, must not be purely numeric (like test_1), and must not be one of these known-vague words: "1", "2", "3", "a", "b", "case", "thing", "it", "x". If it passes all of those checks, it's descriptive only if it also contains an underscore (multiple words) OR is longer than 3 characters. Return True if the name is descriptive by these rules, else False.
name is a non-empty string.
is_descriptive_test_name("test_value_below_low_returns_low") → Trueis_descriptive_test_name("test_1") → Falseis_descriptive_test_name("add_returns_sum") → False (doesn't start with test_)
Show Solution
def is_descriptive_test_name(name):
if not name.startswith("test_"):
return False
body = name[len("test_"):]
if body == "" or body.isdigit():
return False
vague = {"1", "2", "3", "a", "b", "case", "thing", "it", "x"}
if body in vague:
return False
return "_" in body or len(body) > 3