Showing posts with label others. Show all posts
Showing posts with label others. Show all posts

Jan 20, 2009

Given a random number generator that is supposed to behave like a random variable uniformly distributed over [0,1], what tests can you think of that w

Given a random number generator that is supposed to behave like a random variable uniformly distributed over [0,1], what tests can you think of that will measure (in some meaningful way) the randomness of its numbers generated? Do there exist some standard often-applied procedures for this purpose?
As I know to test the random number generator, we should make sure the distribution of generator should be uniform. So I will use a vector to contain the generated random numbers, and sort this vector, cut the vector into some segment, count the elements on each segment, if the counters are roughly close, we can assume the generator is uniform distribution.

Given gray code of a binary number. Write a code to Find the binary number.

Given gray code of a binary number. Write a code to Find the binary number.
g = b ^ (b >> 1);

How many trailing zeros are there in n factorial?

How many trailing zeros are there in n factorial?
number_of_trailing_zeros = floor(n/5) + floor(n/5^2) + floor(n/5^3) + ...

Find a square root of a number without using the square root function. The number is n and we guess the square root is a

Find a square root of a number without using the square root function.
The number is n and we guess the square root is a
While(1)
{
If(a*a==n)
Return a;
a=(a+n/a)/2;
}
If a is square root of n, a^2=n, a=n/a, if a is not square root of n, sr of n must be between a and n/a, so the average b=1/2(a+a/n) create a shorter distance between b and n/b where sr of n lies in, we do the iteration until we get the square root.
Or
1+3=4
1+3+5=9
1+3+5+7=16
….
sum=0;
for(int n=1;sum < num;n++)
sum=sum+(2n-1);
return n;

Swap two numbers without using a temporarily variable.

Swap two numbers without using a temporarily variable.
a=a-b
b=a+b
a=b-a
or
a=a^b
b=a^b
a=a^b

Given two events with integer start and end times, E1 = (s1, e1), E2 = (s2, e2), implement a quick boolean check to see if the events overlap.

Given two events with integer start and end times, E1 = (s1, e1), E2 = (s2, e2), implement a quick boolean check to see if the events overlap.
s1 < e2 && s2 < e1
or
!((e1 < s2)||(e2 < s1))

You need to check that your friend, Bob, has your correct phone number, but you cannot ask him directly. You must write a question on a card which and

You need to check that your friend, Bob, has your correct phone number, but you cannot ask him directly. You must write a question on a card which and give it to Eve who will take the card to Bob and return the answer to you. What must you write on the card, besides the question, to ensure Bob can encode the message so that Eve cannot read your phone number?
ask bob the difference between my number and his number in decimal.
Or
Give bob your public key, and ask bob encrypt his phone no. by this public key and send back. You use your private key to decrypt the message

Jan 19, 2009

What data structure would you use to implement MS Excel? Justify your approach.

What data structure would you use to implement MS Excel? Justify your approach.
use normal 2D matrix, with virtually allocated memory, commiting only the used cells. See VirtualAlloc() function in win32 for details. In this case this is very easy to use and memory consumption is just as needed.