Finding the average of large list of numbers

Himanshu Yadav

Came across this interview question.

Write an algorithm to find the mean(average) of a large list. This list could contain trillions or quadrillions of number. Each number is manageable in hundreds, thousands or millions.

Googling it gave me all Median of Medians solutions. How should I approach this problem?
Is divide and conquer enough to deal with trillions of number?
How to deal with the list of the such a large size?

scenia

If the size of the list is computable, it's really just a matter of how much memory you have available, how long it's supposed to take and how simple the algorithm is supposed to be.
Basically, you can just add everything up and divide by the size.
If you don't have enough memory, dividing first might work (Note that you will probably lose some precision that way).

Another approach would be to recursively split the list into 2 halves and calculating the mean of the sublists' means. Your recursion termination condition is a list size of 1, in which case the mean is simply the only element of the list. If you encounter a list of odd size, make either the first or second sublist longer, this is pretty much arbitrary and doesn't even have to be consistent.

If, however, you list is so giant that its size can't be computed, there's no way to split it into 2 sublists. In that case, the recursive approach works pretty much the other way around. Instead of splitting into 2 lists with n/2 elements, you split into n/2 lists with 2 elements (or rather, calculate their mean immediately). So basically, you calculate the mean of elements 1 and 2, that becomes you new element 1. the mean of 3 and 4 is your new second element, and so on. Then apply the same algorithm to the new list until only 1 element remains. If you encounter a list of odd size, either add an element at the end or ignore the last one. If you add one, you should try to get as close as possible to your expected mean.
While this won't calculate the mean mathematically exactly, for lists of that size, it will be sufficiently close. This is pretty much a mean of means approach. You could also go the median of medians route, in which case you select the median of sublists recursively. The same principles apply, but you will generally want to get an odd number.
You could even combine the approaches and calculate the mean if your list is of even size and the median if it's of odd size. Doing this over many recursion steps will generate a pretty accurate result.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding the average of numbers

From Java

Finding the average of a list

From Dev

Finding the "centered average" of a list

From Dev

Finding and sorting average in list

From Dev

Finding average length of strings in a list

From Dev

finding average with list.fold

From Dev

Finding the average of every sublist in a list

From Dev

Finding average of two numbers using only ints

From Dev

Finding large changes in consecutive numbers in database

From Dev

Finding groups of increasing numbers in a list

From Dev

Finding number of positive numbers in list

From Dev

Finding number of positive numbers in list

From Dev

Finding missing numbers in a list in Perl

From Dev

Finding average of nested list in Common Lisp

From Dev

Finding an average but ignoring any zero in a list [Python]

From Dev

PYTHON: Finding the average of values of a nested list

From Dev

PYTHON: Finding the average of values of a nested list

From Dev

Finding the average from a mapped list of int

From Dev

Average of a list of numbers, stored as strings in a Python list

From Dev

Average of a list of numbers, stored as strings in a Python list

From Dev

Finding Average by Replacing Text Values with Numbers Using Array and Not Let()

From Dev

Finding numbers greater than the average - why is my IF statement not working properly?

From Dev

Finding the Average of all Odd and Even Numbers from an Input/Text File

From Dev

Finding Average by Replacing Text Values with Numbers Using Array and Not Let()

From Dev

Finding a pair of prime numbers with a given arithmetic average using Python

From Dev

Finding average of numbers from one line of a file avoiding the first word

From Dev

Finding name variations from large list Ruby

From Dev

Trying to average a list of numbers using functions

From Dev

How to get the average of all the numbers from the list

Related Related

  1. 1

    Finding the average of numbers

  2. 2

    Finding the average of a list

  3. 3

    Finding the "centered average" of a list

  4. 4

    Finding and sorting average in list

  5. 5

    Finding average length of strings in a list

  6. 6

    finding average with list.fold

  7. 7

    Finding the average of every sublist in a list

  8. 8

    Finding average of two numbers using only ints

  9. 9

    Finding large changes in consecutive numbers in database

  10. 10

    Finding groups of increasing numbers in a list

  11. 11

    Finding number of positive numbers in list

  12. 12

    Finding number of positive numbers in list

  13. 13

    Finding missing numbers in a list in Perl

  14. 14

    Finding average of nested list in Common Lisp

  15. 15

    Finding an average but ignoring any zero in a list [Python]

  16. 16

    PYTHON: Finding the average of values of a nested list

  17. 17

    PYTHON: Finding the average of values of a nested list

  18. 18

    Finding the average from a mapped list of int

  19. 19

    Average of a list of numbers, stored as strings in a Python list

  20. 20

    Average of a list of numbers, stored as strings in a Python list

  21. 21

    Finding Average by Replacing Text Values with Numbers Using Array and Not Let()

  22. 22

    Finding numbers greater than the average - why is my IF statement not working properly?

  23. 23

    Finding the Average of all Odd and Even Numbers from an Input/Text File

  24. 24

    Finding Average by Replacing Text Values with Numbers Using Array and Not Let()

  25. 25

    Finding a pair of prime numbers with a given arithmetic average using Python

  26. 26

    Finding average of numbers from one line of a file avoiding the first word

  27. 27

    Finding name variations from large list Ruby

  28. 28

    Trying to average a list of numbers using functions

  29. 29

    How to get the average of all the numbers from the list

HotTag

Archive