Implement Safe Average Function
Company: Waymo
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This question evaluates proficiency with basic programming fundamentals including numeric computation, handling edge cases such as empty inputs, input immutability, and data type consistency.
Constraints
- 0 <= len(values) <= 100000
- Each element of values is an integer or float
- Values are finite numeric values
- The input list must not be mutated
Examples
Input: ([1, 2, 3],)
Expected Output: 2.0
Explanation: The sum is 6 and there are 3 values, so the average is 6 / 3 = 2.0.
Input: ([-1, 1],)
Expected Output: 0.0
Explanation: The sum is 0 and there are 2 values, so the average is 0 / 2 = 0.0.
Input: ([],)
Expected Output: 0
Explanation: The list is empty, so the function returns 0 instead of dividing by zero.
Input: ([5],)
Expected Output: 5.0
Explanation: A single-element list has an average equal to that element: 5 / 1 = 5.0.
Input: ([1.5, 2.5, 3.0],)
Expected Output: 2.3333333333333335
Explanation: The sum is 7.0 and there are 3 values, so the average is 7.0 / 3.
Hints
- Handle the empty list case before dividing to avoid division by zero.
- You only need the total sum and the number of elements.