PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency with basic programming fundamentals including numeric computation, handling edge cases such as empty inputs, input immutability, and data type consistency.

  • medium
  • Waymo
  • Coding & Algorithms
  • Data Scientist

Implement Safe Average Function

Company: Waymo

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

Write a Python function `average(values)` that returns the arithmetic mean of a list of numbers. Requirements: - If `values` is empty, return `0`. - The input is a flat list of numeric values such as integers or floats. - Do not mutate the input list. Examples: - `average([1, 2, 3])` returns `2.0` - `average([-1, 1])` returns `0.0` - `average([])` returns `0`

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.

Write a function `solution(values)` that returns the arithmetic mean of a flat list of numeric values. The arithmetic mean is the sum of all values divided by the number of values. If the list is empty, return `0`. The function must not mutate the input list.

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

  1. Handle the empty list case before dividing to avoid division by zero.
  2. You only need the total sum and the number of elements.
Last updated: Jun 15, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Validate a Parent-Child Forest - Waymo (medium)
  • Expand Nested Repetition Expressions - Waymo (medium)
  • Find Largest Adjacent Sorted Difference - Waymo (medium)
  • Find Shortest Paths to Target Nodes - Waymo (medium)
  • Serialize Expression Tree Minimizing Parentheses - Waymo (medium)