🐍 Python for Everyone

← Back to practice bank

Chapter 6 Coding Challenges 45-60 min, auto-graded

Coding Challenges: Strings Deep Dive

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 hidden-until-you-look test cases (including edge cases like empty strings). Stuck? Every challenge has a Show Solution reveal at the bottom. Only uses what Chapters 1-6 have covered so far — no lists, no def beyond the function stub already given, no sorted()/sets.

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 Reverse a String Easy

Write a function reverse_string(s) that returns the string s reversed.

Constraints: s may be empty, and may contain any mix of upper/lowercase letters.
Examples:
reverse_string("hello") → "olleh"
reverse_string("a") → "a"
reverse_string("") → ""
challenge_1.py
Show Solution
solution_1.py
def reverse_string(s):
    return s[::-1]

2 Check a Palindrome Easy

Write a function is_palindrome(s) that returns True if s reads the same forwards and backwards, and False otherwise. This check is exact (case-sensitive, no trimming).

Constraints: s may be empty (an empty string counts as a palindrome) or a single character (always a palindrome).
Examples:
is_palindrome("racecar") → True
is_palindrome("hello") → False
is_palindrome("Aa") → False (case-sensitive)
challenge_2.py
Show Solution
solution_2.py
def is_palindrome(s):
    return s == s[::-1]

3 Count the Vowels Easy

Write a function count_vowels(s) that returns how many of the characters in s are vowels (a, e, i, o, u), counting both upper and lowercase.

Constraints: s may be empty, and may mix upper/lowercase letters.
Examples:
count_vowels("hello") → 2
count_vowels("PYTHON") → 1
count_vowels("") → 0
challenge_3.py
Show Solution
solution_3.py
def count_vowels(s):
    count = 0
    for char in s.lower():
        if char in "aeiou":
            count = count + 1
    return count

4 Count a Character Easy

Write a function count_char(s, ch) that returns how many times the single character ch appears in s.

Constraints: s may be empty; ch may not appear in s at all (return 0).
Examples:
count_char("banana", "a") → 3
count_char("banana", "z") → 0
count_char("Mississippi", "s") → 4
challenge_4.py
Show Solution
solution_4.py
def count_char(s, ch):
    return s.count(ch)

5 Is It a Valid Number String? Easy

Write a function is_valid_number(s) that returns True only if every character in s is a digit (0-9) — the kind of check you'd run before calling int(s) on form input.

Constraints: s may be empty, contain a decimal point, a minus sign, or spaces — none of those count as valid.
Examples:
is_valid_number("123") → True
is_valid_number("12.5") → False
is_valid_number("-5") → False
challenge_5.py
Show Solution
solution_5.py
def is_valid_number(s):
    return s.isdigit()

6 Capitalize Each Word Medium

Write a function capitalize_words(sentence) that returns sentence with the first letter of every word uppercase and the rest of each word lowercase, words separated by a single space. Build the result yourself with .split() and a for-loop — don't rely on .title() alone (it mishandles things like apostrophes).

Constraints: sentence may be empty, a single word, or contain extra spaces between words (which .split() already collapses for you).
Examples:
capitalize_words("hello world") → "Hello World"
capitalize_words("PYTHON is FUN") → "Python Is Fun"
capitalize_words("") → ""
challenge_6.py
Show Solution
solution_6.py
def capitalize_words(sentence):
    result = ""
    first = True
    for word in sentence.split():
        piece = word[0].upper() + word[1:].lower()
        if first:
            result = piece
            first = False
        else:
            result = result + " " + piece
    return result

7 Mask a Card Number Medium

Write a function mask_string(s, keep) that replaces every character in s except the last keep characters with "*" — the way a receipt hides most of a credit card number.

Constraints: keep may be 0 (mask everything) or greater than len(s) (nothing to mask — return s unchanged).
Examples:
mask_string("4111111122223333", 4) → "************3333"
mask_string("hello", 2) → "***lo"
mask_string("hi", 4) → "hi"
challenge_7.py
Show Solution
solution_7.py
def mask_string(s, keep):
    masked_len = len(s) - keep
    if masked_len < 0:
        masked_len = 0
    visible_start = len(s) - keep
    if visible_start < 0:
        visible_start = 0
    return "*" * masked_len + s[visible_start:]

8 Anagram Check Medium

Write a function is_anagram(a, b) that returns True if a and b use exactly the same letters the same number of times (ignoring case and spaces), and False otherwise. Solve it using only .lower(), .replace(), len(), .count(), and a for-loop — no sorted(), lists, or sets.

Constraints: comparisons are case-insensitive and ignore spaces; both strings may be empty.
Examples:
is_anagram("listen", "silent") → True
is_anagram("hello", "world") → False
is_anagram("Dormitory", "Dirty Room") → True
challenge_8.py
Show Solution
solution_8.py
def is_anagram(a, b):
    a_clean = a.lower().replace(" ", "")
    b_clean = b.lower().replace(" ", "")
    if len(a_clean) != len(b_clean):
        return False
    for ch in a_clean:
        if a_clean.count(ch) != b_clean.count(ch):
            return False
    return True
← Practice Bank Interview Questions →