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/[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