🐍 Python for Everyone

← Back to practice bank

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

Coding Challenges: Dictionaries & Sets

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 empty dicts and sets). Stuck? Every challenge has a Show Solution reveal at the bottom. Only uses what Chapters 1-8 have covered so far — no comprehensions, no def beyond the function stub already given.

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 Word Frequency Counter Easy

Write a function word_frequency(words) that returns a dictionary mapping each word in words to how many times it appears.

Constraints: words may be empty (return {}); words may repeat any number of times.
Examples:
word_frequency(["a", "b", "a", "c", "b", "a"]) → {"a": 3, "b": 2, "c": 1}
word_frequency([]) → {}
word_frequency(["x"]) → {"x": 1}
challenge_1.py
Show Solution
solution_1.py
def word_frequency(words):
    counts = {}
    for word in words:
        counts[word] = counts.get(word, 0) + 1
    return counts

2 Find Duplicate Values Easy

Write a function find_duplicate_values(d) that returns a sorted list of keys in d whose value also appears under an earlier key (i.e. two or more keys share the same value). Each duplicate key should appear once in the result.

Constraints: d may be empty (return []); values may be numbers or strings; return the result sorted.
Examples:
find_duplicate_values({"a": 1, "b": 2, "c": 1}) → ["c"]
find_duplicate_values({"a": 1, "b": 2, "c": 3}) → []
find_duplicate_values({"a": 1, "b": 1, "c": 1}) → ["b", "c"]
challenge_2.py
Show Solution
solution_2.py
def find_duplicate_values(d):
    seen_values = []
    duplicate_keys = []
    for key in d:
        value = d[key]
        if value in seen_values:
            duplicate_keys.append(key)
        else:
            seen_values.append(value)
    duplicate_keys.sort()
    return duplicate_keys

3 Merge Two Dicts, Summing Shared Keys Easy

Write a function merge_sum_shared_keys(a, b) that merges dictionaries a and b into one new dictionary. If a key exists in both, its value in the result should be the sum of both values; otherwise the key keeps its original value.

Constraints: either dict may be empty; values are always numbers.
Examples:
merge_sum_shared_keys({"a": 1, "b": 2}, {"b": 3, "c": 4}) → {"a": 1, "b": 5, "c": 4}
merge_sum_shared_keys({}, {"a": 1}) → {"a": 1}
merge_sum_shared_keys({"a": 1}, {}) → {"a": 1}
challenge_3.py
Show Solution
solution_3.py
def merge_sum_shared_keys(a, b):
    result = {}
    for key in a:
        result[key] = a[key]
    for key in b:
        if key in result:
            result[key] = result[key] + b[key]
        else:
            result[key] = b[key]
    return result

4 Invert a Dictionary Easy

Write a function invert_dict(d) that returns a new dictionary with d's keys and values swapped — each value becomes a key, mapped back to its original key.

Constraints: d may be empty; if two keys share the same value, the later key (in iteration order) wins in the inverted result.
Examples:
invert_dict({"a": "x", "b": "y"}) → {"x": "a", "y": "b"}
invert_dict({}) → {}
invert_dict({"x": "y"}) → {"y": "x"}
challenge_4.py
Show Solution
solution_4.py
def invert_dict(d):
    result = {}
    for key in d:
        result[d[key]] = key
    return result

5 Common Elements Between Two Sets Medium

Write a function common_elements(a, b) that takes two lists, converts them to sets, and returns a sorted list of the elements common to both.

Constraints: either list may be empty; lists may contain duplicate values or strings; return the result sorted.
Examples:
common_elements([1, 2, 3], [2, 3, 4]) → [2, 3]
common_elements([1, 2], [3, 4]) → []
common_elements([1, 1, 2], [1, 2, 2]) → [1, 2]
challenge_5.py
Show Solution
solution_5.py
def common_elements(a, b):
    set_a = set(a)
    set_b = set(b)
    common = set_a & set_b
    result = list(common)
    result.sort()
    return result

6 Group Records By a Key Medium

Write a function group_by_key(records, key) that takes a list of dictionaries (records) and groups them into a dictionary mapping each distinct value of records[i][key] to a list of the full records that had that value, in original order.

Constraints: records may be empty (return {}); every record is guaranteed to contain key.
Examples:
group_by_key([{"dept": "eng"}, {"dept": "sales"}, {"dept": "eng"}], "dept") → {"eng": [{"dept": "eng"}, {"dept": "eng"}], "sales": [{"dept": "sales"}]}
group_by_key([], "dept") → {}
challenge_6.py
Show Solution
solution_6.py
def group_by_key(records, key):
    groups = {}
    for record in records:
        group_value = record[key]
        if group_value in groups:
            groups[group_value].append(record)
        else:
            groups[group_value] = [record]
    return groups

7 Check if Two Words Are Anagrams Medium

Write a function is_anagram(word_a, word_b) that returns True if word_a and word_b are anagrams of each other (contain exactly the same letters, same counts, in any order), using a dictionary of letter counts — not sorted().

Constraints: both words may be empty (two empty strings are anagrams of each other); comparison is case-sensitive.
Examples:
is_anagram("listen", "silent") → True
is_anagram("hello", "world") → False
is_anagram("", "") → True
challenge_7.py
Show Solution
solution_7.py
def is_anagram(word_a, word_b):
    counts_a = {}
    for ch in word_a:
        counts_a[ch] = counts_a.get(ch, 0) + 1
    counts_b = {}
    for ch in word_b:
        counts_b[ch] = counts_b.get(ch, 0) + 1
    return counts_a == counts_b

8 Find the Most Frequent Element Medium

Write a function most_frequent(items) that returns the value in items that appears the most times. If there's a tie, return whichever of the tied values appears first in items.

Constraints: items always has at least one element; values may be numbers or strings.
Examples:
most_frequent([1, 2, 2, 3, 2]) → 2
most_frequent(["a"]) → "a"
most_frequent([1, 1, 2, 2]) → 1
challenge_8.py
Show Solution
solution_8.py
def most_frequent(items):
    counts = {}
    for item in items:
        counts[item] = counts.get(item, 0) + 1
    best_item = None
    best_count = -1
    for item in items:
        if counts[item] > best_count:
            best_count = counts[item]
            best_item = item
    return best_item
← Practice Bank Interview Questions →