Re: some interview questions
- From: "Jim Gibson" <james.t.gibson@xxxxxxxxxxxxx>
- Date: 10 Sep 2006 23:21:57 -0700
For a solution to 1., how about this ...
int random17( )
{
int sum = 0;
int result;
// call function 7 times, summing the result [1..5]
for( int i = 0; i < 7; ++i)
sum += random15( );
// integer division truncates quotient
result = sum / 5;
// round up
if( (sum % 7) > 3 )
++result;
return result;
}
Digital Puer wrote:
I got these questions on computer job interviews but could not
figure them out.
1. 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 came up with a function that returns a random integer between
1 and 7, but it is not uniformly distributed. Basically, re-map 1 to 5
to be 0 to 4, then add two of those random numbers together
to get a value between 0 to 8, then do modulo 7, returning a
number between 0 and 6, and then re-map to 1 to 7. This
solution more heavily favours 0 and 1; I do not know how
to make a solution with uniform distribution.
2. Suppose you have a function that returns random 64-bit integers
without checking if a particular integer has already been used. How
many integers would be returned before the probability that an integer
has been repeated is above 0.5?
This is probably similar to the birthday problem, which I didn't
quite remember.
.
- Follow-Ups:
- Re: some interview questions
- From: M.J.T. Guy
- Re: some interview questions
- References:
- some interview questions
- From: Digital Puer
- some interview questions
- Prev by Date: Re: algebra with field of quotients.
- Next by Date: Re: Linear Algebra: Symmetric and Skew-symmetric matrix
- Previous by thread: Re: some interview questions
- Next by thread: Re: some interview questions
- Index(es):
Relevant Pages
|