Dict Key Existence vs Falsy Value

Hard ⏱ 18 min 25% acceptance ★★★★☆ 4.3
Write a function describe_field(record, key) that inspects a dictionary record for key and returns: "missing" if the key does not exist at all, "present but empty/zero/false" if the key exists but its value is falsy (e.g. 0, "", None, False, []), or "present with value" otherwise. This distinguishes "key absent" from "key present but empty" — a common source of bugs.

Examples

Example 1
Input
describe_field({"name": "Sam"}, "age")
Output
'missing'
Explanation

"age" is not a key in the dict at all.

Example 2
Input
describe_field({"age": 0}, "age")
Output
'present but empty/zero/false'
Explanation

"age" exists but its value 0 is falsy.

Example 3
Input
describe_field({"age": 30}, "age")
Output
'present with value'
Explanation

Key exists with a truthy value.

Constraints

  • record is a plain dict; key is a string.

Topics

Variables & Data Typesdictionaries

Companies

AmazonSalesforce

Hints

Hint 1

Use `key in record` to test existence separately from the value.

Hint 2

Only check truthiness after confirming the key exists.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.