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.

One pass, identity check against `None`
def count_nulls(values):
    count = 0
    for value in values:
        if value is None:
            count += 1
    return count
Truthiness check
if not value counts 0, False, '' and None alike, so [0, False, '', None] comes back as 4 gaps.
Identity check
if value is None counts only the dropped reading, so the same list comes back as 1.

Common follow-up questions