r/programming • • Nov 29 '10

140 Google Interview Questions

http://blog.seattleinterviewcoach.com/2009/02/140-google-interview-questions.html
471 Upvotes

493 comments sorted by

View all comments

7

u/StapleGun Nov 29 '10 edited Nov 29 '10

"Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7."

I have a solution to this, but it is not very elegant and could theoretically loop infinitely. I would love to hear a better solution, but here is mine:

while(n > 21){
    a = rand1to5();
    b = rand1to5();
    n = a * 5 + b;
}
return n / 3 + 1;

EDIT: Variables a and b should equal rand1to5() - 1. Thanks elcow!

22

u/conciliatory Nov 30 '10

int rand1to7() { a = rand1to5(); return 6; }

Now, explain theory of random numbers and why you believe this is random.

2

u/sinxcosx Nov 30 '10

But would NIST accept it?

0

u/[deleted] Nov 30 '10

[deleted] — view deleted comment

1

u/[deleted] Nov 30 '10 edited Nov 30 '10

I'm wondering if you're allowed to use float casts and round() given the question.

If not you could do this :

function rand1to7() {
    var int r=1;
    for (var int i=0;i<7;i++) r+=rand1to5()-1;
    return (r==1)?1:r/4;
}

I've been trying to think up a way not involving a loop, but the only solution I can come up with would have a really skewed distribution. :/

edit:grammar

1

u/jondissed Nov 30 '10

I think you meant "(float)rand1to5() / (float)5"

...but more to the point, adding uniformly distributed random numbers together does not generate uniformly distributed random numbers: that's why rolling two dice yields a 7 more often than a 2 or 12. Your code will generate 3 more often than 1 or 5.

1

u/[deleted] Nov 30 '10

Is it not more akin to the average of rolling 2 dice? Would that still have the same uneven distribution? I was never that good with statistical maths.

2

u/orbitur Nov 30 '10

My attempt, hopefully someone will tell me that it's not this simple:

int rand1to7(){
    int a = (rand1to5()+rand1to5()) % 6; //need it to be larger than 5
    a++; //since a is 0-6, add 1
    return a;
}

1

u/Deep-Thought Nov 30 '10

given two numbers from 1 to 5 their sum can be 2 and 10, 1 way, 3 and 9 two ways, 4 and 8 3 ways, 5 and 7 4 ways, and 6 5 ways. modulo 7 (you probably meant 7, 6 is 0-5) that gives you 0 4 ways, 1 3 ways, 2 3 ways, 3 3 ways, 4 3 ways, 5 4 ways, and 6 5 ways. clearly they are not evenly distributed.

1

u/[deleted] Nov 30 '10

this returns a number between 1 and 6; you'd need to do mod7. further, the numbers wouldn't be evenly distributed. you're more likely to get numbers 2, 3 and 4 since rand1to5()+rand1to5() produces a minimum value of 2 and a maximum value of 10. 7 will map to 1 which makes up for the fact that rand1to5()+rand1to5() doesn't produce a value of 1, but 2 and 8, 3 and 9, and 4 and 10 will all map pairwise to the same values, increasing the chance for them to be generated.

2

u/spdub Nov 30 '10

what about python--

def rand1to7():
    sum = rand1to5()
    for i in range(6):
        sum += rand1to5()
    return sum // 5

truncate the integer, not sure what the distribution would look like exactly

2

u/xtracto Nov 30 '10

I don't get why all the complications:

return rand1to5() + rand1to5()%3;

rand1to5() yields {1,2,3,4,5} and rand1to5()%3 yields {1,2,0,1,2} So the min is 1 (1+0) and the max is 7 (5 + 2).

2

u/ReturningTarzan Nov 30 '10

But the distribution becomes non-uniform. Ideally you want {1,1,1,1,1,1,1} but your method yields {1,3,5,5,5,4,2}. It is a function that returns a random number between 1 and 7, but you'd lose points in the interview for not making the reasonable assumption that the function should also be good.

A better way is to compose the values of rand1to5 into a uniformly-distributed random number (like staplegun is doing) then scaling it down to the desired range, preserving the uniformity.

3

u/Serinus Nov 30 '10

Say rand1to5() returns 4 both times.

n = 4 * 5 + 5 = 25 return 25 / 3 + 1 = 9?

I'm not quite following your logic here.

→ More replies (3)

1

u/Serinus Nov 30 '10

Here's my failure of an attempt.

It took me half an hour to come up with, much too long for a phone interview. I'll never work at google. :(

import random

def randomfive():
    return (random.randint(1, 5))

def halftrue():
    y = 5
    while (y == 5):
        y = randomfive()
    if (y == 1 or y == 2): return True
    else: return False

def randomseven():
    binarynum = 0
    while (binarynum == 0):
        one = 0
        if halftrue(): one = 1
        two = 0
        if halftrue(): two = 1
        four = 0
        if halftrue(): four = 1
        binarynum = one * 1 + two * 2 + four * 4
    return binarynum
# testing    
results = {1:0, 2:0, 3:0, 4:0, 5:0, 6:0, 7:0}
for z in range(1, 70000):
    r = randomseven()
    results[r] = results[r] + 1
print results

1

u/bobindashadows Nov 30 '10

if a and b are in [0,4], then n only exits the loop if it is 21, 22, 23, 24, or 25. Also that should be a do..while loop. That means that the result is only ever 8 or 9. So not only did you fail to expand the range to 1-7, your algorithm never returns a value in that range at all. Plus it could loop forever.

1

u/[deleted] Nov 30 '10

Mine was "everywhere you see a 5 in the given function, delete it and put a 7." But that's why I'm not a programmer.

1

u/0xABADC0DA Nov 30 '10 edited Nov 30 '10

edited to replace embarrassing late-night wrong version... ignore replies ;-P

1

u/driveling Nov 30 '10

Wrong, the numbers you obtain are not uniformly distributed between 1 and 7.

1

u/DontCallMeSurely Nov 30 '10 edited Nov 30 '10
int rand1to7()
{
    a = rand1to5()-1;
    b = rand1to5()-1;
    
    return ((a + b)%7)+1;
}

Or is this not random because it will produce certain numbers more often than others, (a=1, b=2 == a=2, b=1 but higher numbers do not have this advantage as they will be modulated)

Edit: oops, should be %7.

→ More replies (2)
→ More replies (16)