🐍 Python for Everyone

← Back to practice bank

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

Coding Challenges: Working with APIs & JSON

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). Stuck? Every challenge has a Show Solution reveal at the bottom. Every challenge takes a JSON string or plain data as input — none of them need a live network call. Only uses what Chapters 1-19 have covered so far. No import beyond json/os/re/math/datetime/random/csv.

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 Classify a Status Code Easy

Write a function parse_response_status(status_code) that returns "success" if 200 <= status_code < 300, "client_error" if 400 <= status_code < 500, "server_error" if 500 <= status_code < 600, and "unknown" for anything else.

Constraints: status_code is always an integer.
Examples:
parse_response_status(200) → "success"
parse_response_status(404) → "client_error"
challenge_1.py
Show Solution
solution_1.py
def parse_response_status(status_code):
    if 200 <= status_code < 300:
        return "success"
    elif 400 <= status_code < 500:
        return "client_error"
    elif 500 <= status_code < 600:
        return "server_error"
    return "unknown"

2 Read One Field From JSON Text Easy

Write a function parse_json_field(json_text, field) that parses json_text with json.loads() and returns the value of field, or None if that field isn't present.

Constraints: json_text is always valid JSON representing a flat object.
Examples:
parse_json_field('{"city": "Austin"}', "city") → "Austin"
parse_json_field('{"city": "Austin"}', "humidity") → null
challenge_2.py
Show Solution
solution_2.py
import json

def parse_json_field(json_text, field):
    data = json.loads(json_text)
    return data.get(field)

3 Pretty-Print JSON Easy

Write a function pretty_print_json(data) that returns data (a Python dict) converted to an indented JSON string, using json.dumps(data, indent=2).

Constraints: data is always a JSON-serializable dict.
Examples:
pretty_print_json({"a": 1}) → '{\n "a": 1\n}'
challenge_3.py
Show Solution
solution_3.py
import json

def pretty_print_json(data):
    return json.dumps(data, indent=2)

4 Get a Deeply Nested Value Medium

Write a function get_nested_value(data, keys) that navigates data (a nested dict/list structure) one step at a time for every key or index in the list keys, and returns whatever value is reached at the end.

Constraints: keys may contain a mix of dict keys (strings) and list indexes (integers); the full path is always valid for the given data.
Examples:
get_nested_value({"a": {"b": {"c": 42}}}, ["a", "b", "c"]) → 42
get_nested_value({"items": [{"name": "x"}, {"name": "y"}]}, ["items", 1, "name"]) → "y"
challenge_4.py
Show Solution
solution_4.py
def get_nested_value(data, keys):
    current = data
    for key in keys:
        current = current[key]
    return current

5 Safe Field Access Easy

Write a function safe_field(data, key, default) that returns data[key] if present, otherwise returns default, using dict.get().

Constraints: data is always a flat dict.
Examples:
safe_field({"a": 1}, "a", 0) → 1
safe_field({"a": 1}, "b", 0) → 0
challenge_5.py
Show Solution
solution_5.py
def safe_field(data, key, default):
    return data.get(key, default)

6 Build a Query String Medium

Write a function build_query_string(params) that builds a query string from the dict params, in the form "key1=value1&key2=value2", preserving the dict's insertion order. Return an empty string if params is empty.

Constraints: params values may be strings, integers, or booleans.
Examples:
build_query_string({"a": 1, "b": 2}) → "a=1&b=2"
build_query_string({}) → ""
challenge_6.py
Show Solution
solution_6.py
def build_query_string(params):
    return "&".join(f"{k}={v}" for k, v in params.items())

7 Sum a Field Across a List of Records Medium

Write a function sum_nested_field(records, key) that returns the sum of record[key] across every dict in the list records. Return 0 if records is empty.

Constraints: every dict in records is guaranteed to have key.
Examples:
sum_nested_field([{"amount": 10}, {"amount": 20}], "amount") → 30
sum_nested_field([], "amount") → 0
challenge_7.py
Show Solution
solution_7.py
def sum_nested_field(records, key):
    return sum(record[key] for record in records)

8 Fetch-Parse-Format Mini Pipeline Medium

Write a function fetch_parse_format(raw_json_text, template) that parses raw_json_text with json.loads(), then returns template.format(**data), filling in the template's placeholders from the parsed dict's keys.

Constraints: template's placeholder names always match keys present in the parsed data.
Examples:
fetch_parse_format('{"name": "Ada", "score": 99}', "{name} scored {score}") → "Ada scored 99"
challenge_8.py
Show Solution
solution_8.py
import json

def fetch_parse_format(raw_json_text, template):
    data = json.loads(raw_json_text)
    return template.format(**data)
← Practice Bank Interview Questions →