Problem · Array

Average or Zero

Learn this problem
EasyWaymoOA

Problem statement

Given a numeric array table, return its arithmetic mean.

The arithmetic mean is the sum of all values divided by the number of values. If table is empty, return 0.

Function

average(table: double[]) → double

Examples

Example 1

table = [2,4,6,8]return = 5.0

The values sum to 20. Dividing by the 4 values gives 5.

Example 2

table = []return = 0.0

The input is empty, so the required result is 0.

Example 3

table = [-1.5,2.5,5]return = 2.0

The values sum to 6. Dividing by the 3 values gives 2.

More Waymo problems

drafts saved locally
public double average(double[] table) {
  // Write your code here.
}
table[2,4,6,8]
expected5.0
checking account