Null Counter
An easy Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.
- Domain
- Python
- Difficulty
- easy
- Seniority
- L3
Problem
A sensor dashboard reports far more gaps than the devices ever logged, and the on-call engineer traced it to zero and blank readings being counted as missing. In a flat list of values from one export, where a dropped reading arrives as None, return how many readings were actually dropped.
How many holes in the data?
Example 1
Input:
values = [1,null,3,null,null]
Output:
3
Example 2
Input:
values = []
Output:
0
Worked solution and explanation
This is a sentinel-versus-falsy question wearing a data-quality costume. The skill is counting exactly the values that are the missing marker None, and nothing that merely looks empty. Anyone can write the loop; what separates candidates is the test inside it. Reach for if not value and every legitimate 0 reading, every False flag and every blank string gets reported as a dropped reading, which is exactly the inflated gap count the on-call engineer was chasing.
Walking to the answer
Step 1: Decide what missing means
In this export only None marks a dropped reading. A 0 is a real measurement, so the predicate has to single out None and let every other value through, falsy or not.
Step 2: Ask the identity question with is None
None is a singleton, so value is None asks precisely whether this slot is the missing marker. Truthiness asks a different question, whether the value is empty-ish, and the answers diverge on exactly the readings that matter.
Step 3: Let the counter handle the empty export
Starting count at 0 means an empty values list falls straight through to return count. No guard clause is needed, and none of the empty-list special-casing candidates sometimes bolt on.
def count_nulls(values):
count = 0
for value in values:
if value is None:
count += 1
return count- Truthiness check
if not valuecounts0,False,''andNonealike, so[0, False, '', None]comes back as 4 gaps.- Identity check
if value is Nonecounts only the dropped reading, so the same list comes back as 1.
Common follow-up questions
- What if blank strings should also count as dropped readings? (Tests widening the predicate deliberately, with `value is None or value == ''`, without sliding back into truthiness.)
- How would you report the share of readings that were dropped? (Tests dividing the gap count by `len(values)` and deciding what an empty export returns.)
- What is the difference between `is None` and `== None`? (Tests identity versus equality: `is` compares the object reference, `==` calls `__eq__`.)