Probability Puzzle: Expected Number of Expression Evaluations

Source: Asked for a data scientist position at a technology startup in financial services sector in London

Problem:

Given python code to calculate maximum in an array of integers x

def find_max(x):

max_num = x[0]

for i in x[1:]:
if i > max_num:
max_num = i << Expected number of times this expression was evaluated


return max_num


Calculate the expected number of times the expression max_num = i was evaluated, given that array x was taken from a uniform random distribution.

Comments

  1. answer is 1/2 + 1/3 + 1/4...+ 1/n

    probability that j-th element is the maximum till now = 1/j

    sum over all such j's from 2 to n

    Reply Delete
  2. Assuming total no of elements N. (N+1)/2

    Reply Delete
  3. Assumption : Elements come in random order.
    Consider the ith iteration, the probability that the ith element is the largest amongst the first i elements is simply 1/i since all are equally likely to be largest. So E[no. of times the line is evaluated for ith iteration] = 1/i = E_i. So,
    E[total number of assignments to var max_num] = summation of E_i for i=1 to n. This is approximately LogN for large N.

    Reply Delete
  4. n/2 by there are infinitely many integers on either side of any integer x and thus probability that next number in array is greater than current maximum is 1/2. Thus expected number is n/2

    Reply Delete
  5. Since we are talking about an integer array, the uniform distribution from which the elements are picked is discrete in nature. And if the range of this distribution is finite, the statement "the probability that the ith element is the largest amongst the first i elements is simply 1/i since all are equally likely to be largest" is not true.

    Consider, for example, a range consisting of only a single integer. Then the expected value is clearly 0. Or a range containing less than LogN integers, then the expected value is clearly less than LogN. The argument holds, however, if the probability of repitition of elements in the array is negligible, which is true if the range is infinite or the array contains real numbers instead of integers.

    Reply Delete
  6. This comment has been removed by the author.

    Reply Delete

Post a Comment

[フレーム]

Popular posts from this blog

Buying Dimsums

Source: Alok Goyal (Stellaris VP, Ex-Helion VC) puzzle blog Problem: A fast food restaurant sells dimsums in boxes of 7 and 3. What’s the greatest number of dimsums a person cannot buy. Generalize it for p and q where p and q are relatively prime. I loved the puzzle. Hope you enjoy it too.

Polya's Urn Problem

Puzzle: There are two urns with one ball each. Each of subsequent n-2 balls is placed into one of these urns, with probability proportional to the number of balls already in that urn. What is the expected number of balls in the smaller sized urn? Source: P. Winkler's Puzzles book. (Chapter: Probability). Solution: Highlight the part between the * symbols for the answer. * This problem can be reformulated as the following problem. Suppose I have a stack of black cards and one red card. Initially I take red card in my hand. Now I add black cards randomly between any two cards (so, initially its either above or below red). Note that the probability that I add the card above the red card, when x-1 is the number of cards above red and y-1 is the number of cards below red is x/(x+y). Let the problem be if red card is dividing the black cards into two sets, what is the expected number of black cards in the smaller section. So, we see that the two problems are equivalent. No...

(Advanced) Cheryl's Birthday Puzzle

Source: Sent to me by Prateek Chandra Jha (IIT Bombay) Problem: This problem is inspired by the Cheryl's Birthday Puzzle ( FB Post , Guardian Link ). Paul, Sam and Dean are assigned the task of figuring out two numbers. They get the following information: Both numbers are integers between (including) 1 and 1000 Both numbers may also be identical. Paul is told the product of the two numbers, Sam the sum and Dean the difference. After receiving their number, the following conversation takes place: Paul: I do not know the two numbers. Sam: You did not have to tell me that, I already knew that. Paul: Then I now know the two numbers. Sam: I also know them. Dean: I do not know the two numbers. I can only guess one which may probably be correct but I am not sure. Paul: I know which one you are assuming but it is incorrect. Dean: Ok, I also know the two numbers. What are the two numbers? Disclaimer: Its not a puzzle for 14-15 year olds like Cheryl's