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.
parse_log_timestamp(date_str) parses a "YYYY-MM-DD" date string and returns formatted output. Example: parse_log_timestamp("2026-11-02") → "2026-11-02".
Type error
pop_undo_action(history) pops and returns the last action from history, returning None if history is empty. Example: pop_undo_action([]) → None.
Logic error
add_granted_roles(roles, new_roles) adds all items from new_roles to roles as flat elements. Example: add_granted_roles(["user"], ["admin"]) → ["user", "admin"].
Type error
round_penalty_score(score) rounds score using standard half-away-from-zero rounding. Example: round_penalty_score(-2.5) → -3.
Logic error
pair_users_and_badges(users, badges) pairs users with badges, filling missing values with "none". Example: pair_users_and_badges(["a", "b"], [1]) → [["a", 1], ["b", "none"]].
Logic error
sort_users_by_priority(users) sorts users by numeric priority ascending, placing None priorities at the end. Example: sort_users_by_priority([{"id": 1, "priority": None}, {"id": 2, "priority": 5}]) → [{"id": 2, "priority": 5}, {"id": 1, "priority": None}].
Type error
override_tenant_port(default_config, new_port) overrides db port in a cloned config without mutating default_config. Example: override_tenant_port({'db': {'port': 5432}}, 5433) → [5433, 5432].
Logic error
find_missing_required_keys(provided_keys, required_keys) returns the missing required keys in sorted order. Example: find_missing_required_keys(["id"], ["id", "email"]) → ["email"].
Logic error
strip_url_protocol(url) removes the leading "https://" protocol prefix from a URL string. Example: strip_url_protocol("https://threat.com") → "threat.com".
Logic error
get_duration_in_seconds(days, hours) returns the total duration in seconds as an integer. Example: get_duration_in_seconds(1, 2) → 93600.
Type error
is_base_handler(handler_name) returns True if handler is an instance of BaseHandler or its subclasses. Example: is_base_handler("auth") → True.
Type error
subtract_inventory_counts(stock, sold) subtracts sold items from stock, removing items that reach zero. Example: subtract_inventory_counts({"apple": 2}, {"apple": 2}) → {}.
Logic error
get_retry_limit(override) returns override count, falling back to 3 only if override is None. Example: get_retry_limit(0) → 0.
Logic error
calculate_discounted_price(price, discount) subtracts discount from price rounded to 2 decimal places. Example: calculate_discounted_price(0.3, 0.2) → 0.1.
Logic error
get_sorted_scores(scores) returns a new list containing scores in ascending order. Example: get_sorted_scores([3, 1, 2]) → [1, 2, 3].
Type error
is_valid_numeric_or_string(val) returns True if val is an int, float, or str. Example: is_valid_numeric_or_string(42) → True.
Type error
lookup_cached_score(cache_json, user_id) retrieves the score for integer user_id from serialized JSON cache data. Example: lookup_cached_score('{"1": 95}', 1) → 95.
Type error
wrap_in_tuple(val) wraps val in a tuple and returns the type name "tuple". Example: wrap_in_tuple(42) → "tuple".
Type error
round_order_rating(rating) rounds rating to the nearest integer using standard round-half-up math. Example: round_order_rating(2.5) → 3.
Logic error
extract_grouped_keys(items) returns the unique sorted group keys from items. Example: extract_grouped_keys(["a", "b", "a"]) → ["a", "b"].
Logic error
find_top_score(scores) returns the highest score from scores, returning 0 if scores is empty. Example: find_top_score([]) → 0.
Logic error
are_all_accounts_verified(accounts) returns True only if accounts is non-empty and every account is verified. Example: are_all_accounts_verified([]) → False.
Logic error
calculate_total_pages(total_items, page_size) returns the total number of pages required to display all items. Example: calculate_total_pages(11, 5) → 3.
Logic error
get_setting_with_fallback(config, key, default_val) returns the value of key from config, returning default_val if key is missing or None. Example: get_setting_with_fallback({'retries': None}, 'retries', 3) → 3.
Null / Undefined
deduplicate_preserve_order(items) removes duplicates while preserving the original appearance order. Example: deduplicate_preserve_order(["b", "a", "c", "a"]) → ["b", "a", "c"].
Logic error
Showing 26–50 of 184 Python challenges