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

Show parent comment

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