Quick Overview

Write a function that returns the arithmetic mean of a list of integers and returns 0 for an empty list. A warm-up from a timed data science assessment that tests careful handling of the empty case and floating-point division.

Average of a List of Integers, Returning 0 for an Empty List

Company: Waymo

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

This warm-up comes from a timed online assessment for a data science role. Write a function `average(table)` that returns the arithmetic mean of the numbers in the list `table`. If the list is empty, return 0. ### Function Signature ```python def average(table: list[int]) -> float: ``` ### Rules - For a non-empty list, return the sum of the elements divided by the number of elements, as a floating-point number (IEEE 754 double precision). - For an empty list, return `0.0`. - Do not round the result. ### Constraints - `0 <= len(table) <= 10^5` - `-10^6 <= table[i] <= 10^6` - The sum of the elements is at most `10^11` in absolute value, so it is exact in both 64-bit integers and double precision; the expected result is that exact sum divided by the length, correctly rounded to a double. ### Examples **Example 1** - Input: `table = [3, 4, 8]` - Output: `5.0` **Example 2** - Input: `table = []` - Output: `0.0` - Explanation: An empty list has no mean; the specification asks for 0 instead of an error. **Example 3** - Input: `table = [-2, 2, 1]` - Output: `0.3333333333333333`

Overview: Write a function that returns the arithmetic mean of a list of integers and returns 0 for an empty list. A warm-up from a timed data science assessment that tests careful handling of the empty case and floating-point division.

Write a function `average(table)` that returns the arithmetic mean of the integers in the list `table`. If the list is empty, return `0`. ### Rules - For a non-empty list, return the sum of the elements divided by the number of elements, as a floating-point number (IEEE 754 double precision). - For an empty list, return `0.0`. - Do not round the result: the expected value is the exact sum divided by the length, correctly rounded to a double. Because the sum is an exact integer and the length is exact, a single floating-point division of the sum by the length produces exactly this value in every supported language. ### Constraints - `0 <= len(table) <= 10^5` - `-10^6 <= table[i] <= 10^6` - The sum of the elements is at most `10^11` in absolute value, so it is exact in both 64-bit integers and double precision. The sum can exceed `2^31 - 1` (for example, 3000 copies of `10^6` already sum to `3 * 10^9`), so accumulate it in a 64-bit integer (`long` in Java, `long long` in C++). Each element fits in a 32-bit signed integer, and the return type is a double. ### Example 1 ```text Input: table = [3, 4, 8] Output: 5.0 ``` (3 + 4 + 8) / 3 = 5.0. ### Example 2 ```text Input: table = [] Output: 0.0 ``` An empty list has no mean; the specification asks for `0.0` instead of an error. ### Example 3 ```text Input: table = [-2, 2, 1] Output: 0.3333333333333333 ``` The sum is 1 and the length is 3; 1 / 3 correctly rounded to a double is 0.3333333333333333.

Constraints

  • 0 <= len(table) <= 10^5
  • -10^6 <= table[i] <= 10^6
  • |sum(table)| <= 10^11, so the sum is exact in a 64-bit integer and in a double
  • The sum can exceed 2^31 - 1: accumulate it in a 64-bit integer (long in Java, long long in C++)
  • Return 0.0 for an empty list; otherwise return sum / len as an unrounded IEEE 754 double

Examples

Input: ([3, 4, 8],)

Expected Output: 5.0

Explanation: Source example 1: (3 + 4 + 8) / 3 = 5.0.

Input: ([],)

Expected Output: 0.0

Explanation: Source example 2 / empty input: an empty list returns 0.0 instead of raising.

Hints

  1. Handle the empty list before dividing, since dividing by a length of zero is undefined.
  2. Think about how large the running total can get relative to the range of a 32-bit integer.
  3. Make sure the final division is floating-point division, not integer division: [1, 2] should give 1.5.

Loading coding console...

Show the approach

Approach

Algorithm: if table is empty, return 0.0 immediately. Otherwise accumulate the sum of all elements in a 64-bit integer (Python's arbitrary-precision int, a JavaScript number, a Java long, or a C++ long long), then return that sum converted to a double divided by the length converted to a double.

Why it is exact: every element is an integer, so every partial sum is an integer with absolute value at most 10^11, well below 2^53. Such integers are represented exactly by 64-bit integers and by doubles, so the accumulated total is the exact sum. The length is also exactly representable. IEEE 754 division is correctly rounded, so one division of the exact sum by the exact length yields the exact mean correctly rounded to a double, which is precisely the value the specification asks for. Python's int / int true division is also correctly rounded, so all four languages agree bit for bit.

Pitfalls: a 32-bit accumulator overflows once the sum passes 2^31 - 1 (about 2147 copies of 10^6 are enough), producing a wrong mean; integer or floor division truncates non-integer means such as 1.5 or rounds -1.5 down to -2; rounding the result (for example to two decimals) changes values such as 0.3333333333333333; and forgetting the empty-list check divides by zero (a ZeroDivisionError in Python, NaN in JavaScript and Java, NaN or a crash in C++).

Edge cases: empty list (0.0), a single element (that element as a float), all-equal elements (the repeated value exactly), mixed signs summing to zero (0.0), negative means, and inputs whose sum lies outside the 32-bit range.

Time complexity:
O(n)
Space complexity:
O(1)