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.
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.
words may be empty (return {}); words may repeat any number of times.
word_frequency(["a", "b", "a", "c", "b", "a"]) → {"a": 3, "b": 2, "c": 1}word_frequency([]) → {}word_frequency(["x"]) → {"x": 1}
Show Solution
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.
d may be empty (return []); values may be numbers or strings; return the result sorted.
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"]
Show Solution
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.
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}
Show Solution
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.
d may be empty; if two keys share the same value, the later key (in iteration order) wins in the inverted result.
invert_dict({"a": "x", "b": "y"}) → {"x": "a", "y": "b"}invert_dict({}) → {}invert_dict({"x": "y"}) → {"y": "x"}
Show Solution
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.
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]
Show Solution
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.
records may be empty (return {}); every record is guaranteed to contain key.
group_by_key([{"dept": "eng"}, {"dept": "sales"}, {"dept": "eng"}], "dept") → {"eng": [{"dept": "eng"}, {"dept": "eng"}], "sales": [{"dept": "sales"}]}group_by_key([], "dept") → {}
Show Solution
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().
is_anagram("listen", "silent") → Trueis_anagram("hello", "world") → Falseis_anagram("", "") → True
Show Solution
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.
items always has at least one element; values may be numbers or strings.
most_frequent([1, 2, 2, 3, 2]) → 2most_frequent(["a"]) → "a"most_frequent([1, 1, 2, 2]) → 1
Show Solution
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