Implement a Safe Average Function in Python

Quick Overview

Implement a Python average function that returns zero for an empty list and explain its time and space complexity. Clarify numeric types, missing values, NaN behavior, streaming alternatives, precision concerns, and the tests needed for a reliable data utility.

Implement a Safe Average Function in Python

Company: Waymo

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Online Assessment

Write a Python function `average(table)` that returns the arithmetic mean of a list of numbers and returns `0` when the list is empty. Explain the function's behavior, complexity, and any input assumptions you would clarify before using it in a data pipeline. ### Constraints & Assumptions - The required empty-list result is exactly `0`. - A direct implementation with Python's built-in numeric operations is sufficient. - Do not silently invent missing-value semantics; explain how `None` or `NaN` would be handled if they can appear. ### Clarifying Questions to Ask - Are integers and floating-point values both valid? - Can the input contain `None`, `NaN`, nonnumeric values, or an iterator rather than a list? - Should the function return an integer or floating-point zero for an empty input? - Is numerical overflow or precision important for the expected data size? ### What a Strong Answer Covers - A correct empty-input branch and correct arithmetic mean for nonempty input. - Time complexity proportional to the number of values and constant auxiliary space for a list input. - Explicit behavior for invalid or missing values rather than accidental coercion. - Awareness that production data work may use library functions with different missing-value rules. ### Follow-up Questions 1. How would you adapt the function to ignore `None` values? 2. How would you compute the mean of a stream that cannot be stored in memory? 3. What happens when the input contains `float('nan')`? 4. How would you test this function?

Overview: Implement a Python average function that returns zero for an empty list and explain its time and space complexity. Clarify numeric types, missing values, NaN behavior, streaming alternatives, precision concerns, and the tests needed for a reliable data utility.

|Home/Data Manipulation (SQL/Python)/Waymo
Waymo logo
Waymo
Mar 14, 2026
mediumData ScientistOnline AssessmentData Manipulation (SQL/Python)
11
0

Write a Python function average(table) that returns the arithmetic mean of a list of numbers and returns 0 when the list is empty. Explain the function's behavior, complexity, and any input assumptions you would clarify before using it in a data pipeline.

Constraints & Assumptions

  • The required empty-list result is exactly 0 .
  • A direct implementation with Python's built-in numeric operations is sufficient.
  • Do not silently invent missing-value semantics; explain how None or NaN would be handled if they can appear.

Clarifying Questions to Ask Guidance

  • Are integers and floating-point values both valid?
  • Can the input contain None , NaN , nonnumeric values, or an iterator rather than a list?
  • Should the function return an integer or floating-point zero for an empty input?
  • Is numerical overflow or precision important for the expected data size?

What a Strong Answer Covers Guidance

  • A correct empty-input branch and correct arithmetic mean for nonempty input.
  • Time complexity proportional to the number of values and constant auxiliary space for a list input.
  • Explicit behavior for invalid or missing values rather than accidental coercion.
  • Awareness that production data work may use library functions with different missing-value rules.

Follow-up Questions Guidance

  1. How would you adapt the function to ignore None values?
  2. How would you compute the mean of a stream that cannot be stored in memory?
  3. What happens when the input contains float('nan') ?
  4. How would you test this function?
Loading comments...