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.
Hints
- Handle the empty list case before dividing to avoid division by zero.
- You only need the total sum and the number of elements.