Biggest gap between numbers comes out as -1
largest_gap(nums) returns the biggest jump between neighbouring numbers in a list that goes up. Example: largest_gap([1, 5, 6]) → 4.
Explore 184 Python challenges. Filter by bug pattern or difficulty.
add_member(teams, team, name) adds a person to a team, creating the team if needed, and returns its members. Example: add_member({}, "blue", "zoe") → ["zoe"].
Other
position_of(queue, name) returns someone's place in line (1 for the front), or 0 if they are not in it. Example: position_of(["asha", "ravi"], "zoe") → 0.
Null / Undefined
headline(text) makes the first letter of each word a capital. Example: headline("don't stop") → "Don't Stop".
Other
stairs(n) builds a staircase of n steps. Example: stairs(3) → ["#", "##", "###"].
Off-by-one
to_cents(price_text) turns a price like "19.99" into whole cents. Example: to_cents("19.99") → 1999.
Type error
has_word(text, word) returns True only when word appears as a whole word. Example: has_word("cat nap", "cat") → True, has_word("I love cats", "cat") → False.
Logic error
leave_days(start_iso, end_iso) counts the days of leave, including the first and last day. Example: leave_days("2024-01-01", "2024-01-07") → 7.
Off-by-one
count_passing(scores) counts the scores of 40 or more. Example: count_passing([72, 35, 90]) → 2.
Logic error
is_newer(version, current) returns True if version is a later release. Example: is_newer("10.0", "9.4") → True.
Type error
reverse_text(text) returns the text backwards. Example: reverse_text("hello") → "olleh".
Type error
double_all(prices) doubles every price. Example: double_all([1, 2, 3]) → [2, 4, 6].
Logic error
An AI assistant wrote this for a classic interview question. nums has every number from 1 to n except one. missing_number(nums, n) returns the missing one. Example: missing_number([1, 2, 4], 4) → 3.
Off-by-one
longest_word(words) returns the longest word. Example: longest_word(["hi", "hello", "hey"]) → "hello".
Logic error
best_score(scores) returns the highest score, or 0 when nobody has played. Example: best_score([3, 9, 4]) → 9, best_score([]) → 0.
Null / Undefined
get_reversed_audit_log(logs) returns a new list with logs in reverse chronological order. Example: get_reversed_audit_log(["a", "b"]) → ["b", "a"].
Type error
is_valid_cart_container(items) returns True if cart was successfully initialized as an object, even if items is empty. Example: is_valid_cart_container([]) → True.
Logic error
safe_find_index(items, target) returns the index of target in items, returning -1 if missing. Example: safe_find_index([1, 2], 99) → -1.
Logic error
build_two_contexts(id1, id2) creates two independent session contexts with their respective user IDs. Example: build_two_contexts(10, 20) → [10, 20].
Scope error
create_user_role_buckets(roles, initial_admin) initializes an empty list for each role and adds initial_admin to the first role. Example: create_user_role_buckets(["admin", "guest"], "alice") → {"admin": ["alice"], "guest": []}.
Logic error
parse_python_dict_payload(payload_str) parses a Python literal dictionary string with single quotes into a dict. Example: parse_python_dict_payload("{'id': 1}") → {"id": 1}.
Type error
verify_is_exception_class(cls_obj) returns True if cls_obj is a class inheriting from Exception, returning False for instances or non-classes. Example: verify_is_exception_class('ValueError') → False.
Type error
calculate_net_payout(gross_amount, processing_fee) subtracts processing_fee from gross_amount rounded to 2 decimal places. Example: calculate_net_payout(1.1, 0.9) → 0.2.
Logic error
count_file_lines(file_content) counts lines in file_content, returning 0 for empty strings. Example: count_file_lines('a\nb\n') → 2.
Logic error
find_lowest_fee(fees) returns the lowest fee from fees, returning 0 if the list is empty. Example: find_lowest_fee([]) → 0.
Logic error
concatenate_words(words) concatenates a list of words into a single string. Example: concatenate_words(["code", "review"]) → "codereview".
Type error
Showing 1–25 of 184 Python challenges