Google Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
1
of 1 vote

def rand5():
    return ((3 * (rand3() - 1) + rand(3)) % 5) + 1

The initial part -

(3 * (rand3() - 1) + rand(3)

- generates numbers uniformly from 0-9. Mod by 5 now means the numbers are uniform from 0-4. Add 1 and now they're uniform 1-5.

Maximum and average number of trials is 1.

- Ryan August 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is wrong.

The initial part - (3 * (rand3()-1) + rand3()) generates numbers uniformly from 0-8. Mod by 5 now means numbers are non-uniform 0-4.

- Anonymous December 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Link to the correct solution.

- Shahbaaz December 31, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

this wont work:
rand5(){
return 2*rand3()%5;
}

number 3,5 will never show up

- win April 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

expected number of trials is 2.4

think bit manipulation

- win April 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@win : Can you elaborate ??

- Anonymous September 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

rand5(){  return (rand3()+rand3())%(3+3); }

- bp September 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More