FastPrepAverage of a Fixed-Size Array

Average of a Fixed-Size Array

Tesla logoTesla● EasyINTERNPHONE SCREEN
Learn

Problem statement

Given a non-empty fixed-size array of integers numbers, return its arithmetic mean.

The arithmetic mean is the sum of all values divided by the number of values.

Function

calculateAverage(numbers: int[]) → double

Examples

Example 1

numbers = [1,2,3,4]return = 2.5

The sum is 10, and 10 / 4 = 2.5.

Example 2

numbers = [7]return = 7.0

A single-element array has that element as its mean.

Example 3

numbers = [-5,0,5,10]return = 2.5

The values sum to 10, so the mean is 10 / 4 = 2.5.

Constraints

  • 1 <= numbers.length <= 10^5.
  • -10^9 <= numbers[i] <= 10^9.
  • The sum of all values fits in a signed 64-bit integer.

More Tesla problems

See Tesla hiring insights
public double calculateAverage(int[] numbers) {
  // write your code here
}
numbers[1,2,3,4]
expected2.5
Checking account…