Interview Question for Interns


Country: India
Interview Type: Written Test




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

The hard part is how to make sure the rand7() gives equally random probability.

The easiest way is to create a look-up table, where the table stores numbers from 1 to 7 with equal probability. And then think of a way to map rand5() results into the look-up table, with equal chance of mapping to any entry.

Thus, we can design a look-up table of size 5x5 as follow, where each number from 1 to 7 appears 3 times each:

Table = { 1, 1, 1, 2, 2,
		  2, 3, 3, 3, 4,
		  4, 4, 5, 5, 5,
		  6, 6, 6, 7, 7,
		  7, 8, 8, 8, 8
		}

Rand7() can be following:

rand7(){
	do{
		row = rand5() -1;
		col = rand5() -1;
		r7  = Table[row][col];
	} while (r7 > 7);

return r7;
}

We can see that row, col are equally likely between 0 to 4. Thus, each entry of the Table is chosen equally likely.
The loop condition while(r7>7) make sure that we only generate numbers from 1 to 7.

- ninhnnsoc February 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

const int MAX = 5*5;
const int DIV = MAX/7;

int rand7()
{
    int num = (rand5()-1)*5+rand5()-1; // randomly distributed in 0:24
    return (num<MAX) ? (num/DIV) : rand7();
}

- ZZZ February 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This doesn't work. Please check your work before posting a "solution".

- tjcbs2 March 03, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this works!

Only need to revise the return statement:
return (num<21) ? 1+(num/DIV) : rand7();

- ninhnnsoc March 07, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int rand7()
{
var Input = new int[] { 1, 2, 3, 4, 5, 6, 7 };
var index = rand5();
while (CountNonZero(Input,7) > 1)
{
index += rand5();
index = index % 7;
if (Input[index] > 0)
Input[index] = 0;
else
{
while (Input[index] == 0)
{
index++;
index = index % 7;
}
Input[index] = 0;
if (CountNonZero(Input, 7) == 1)
break;
}
}
foreach (var v in Input)
if (v > 0) return v;
throw new NotImplementedException("something is wrong with rand7()!");
}

static private int CountNonZero(int[] input, int count)
{
var ret = 0;

foreach (var v in input)
if (v > 0) ret++;

return ret;
}

- Richard Xia March 06, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Python :

import random

def rnd():
     r = random.randint(1, 7)
     return r

- shikhar.tech March 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you have to do it using the function rand5()

- Anonymous November 25, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You have to do it using the function rand5()

- Python expert November 25, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def rand2():
    toss = rand5()
    for i in range(100):
        if toss != 3:
            break
        toss = rand5()
    
    if toss < 3:
        return 1
    return 2

def rand7():
    x = 0
    for i in range(7):
        if rand2() == 1 :
            x += 3.5 / pow(2,i)
    return (int)(math.ceil(x))

- Mor March 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def rand2():
    toss = rand5()
    for i in range(100):
        if toss != 3:
            break
        toss = rand5()
    
    if toss < 3:
        return 1
    return 2

def rand7():
    x = 0
    for i in range(7):
        if rand2() == 1 :
            x += 3.5 / pow(2,i)
    return (int)(math.ceil(x))

- Mor March 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

multiply the output of rand5() by 7/5

- Anonymous March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

rand7() {
	int i = 0; sum = 0;
	while (i < 7) {
		sum += rand5();
	}
	return (sum / 5);
   }

- Sandip K March 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int rand7() {
int i=0;sum=0;
while (i++ < 7) { sum+= rand5(); }
return (sum / 5);
}

- Sandip K March 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Adding rand5() twice and modulus 7. if 0 then return 7 , since range should be between 1 - 7

static int rand7() {
	int r = (rand5() + rand5()) % 7;
	if (r == 0) {
		r = 7;
	}

	return r;
}

- srinath March 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it doesn't work. With this approach, numbers from 1 to 4 have a probability of 3/25; number 5 and 6 have a probability of 4/25; and number 6 has a probability of 5/25.

It's like using 2 regular dices and add both values: the number 7 has the most probability, later 6 and 8, later 5 and 9...and so on

- JulGor February 09, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hope you like this:

import random

def rand5():
	return random.randint(1,5)

def rand7():
	print random.randint(rand5(), 7)

rand7()

- Rajesh April 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Have a binary array of 5x7 slots, and call the rand5 7 times, putting the result into the 1st, 2nd, etc 5-bit sub-array. This creates an equal distribution for all over the 35 slots, and if you think the other way (7-bit subarrays) the distribution is the same good (equal probablity) on those slots, too. So getting 7 values from rand5 will result in having 5 rand7 values in the array.

When a new rand7 value is needed get the number(s) from the 7-bit slots.
Do it 5 times - then all is used - so generate the array again.

Coding is left to the reader as an exercise :)

- Selmeczy, Péter April 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To ensure that the new function has a uniform distribution we can do this:

1) We are going to treat the 7 as a binary number 111
2) We are going to call rand5() for every single bit the seven has (3).
3) If the dice give us a {1,2} the bit is 0
If the dice give us a {3,4} the bit is 1
If the dice give us a 5 we call rand5() again.

4) If we end up with a 000 combination, we repeat the whole process because the result needs to be between 1 and 7.

This doesn't have a great performance, but I think this exercise cares more about the uniformity of the probability function than the execution time of it.

- pablodmusumeci June 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A person's first inclination would be to use the modulo operator. However, the problem with using the modulo operator here is you won't get a uniform distribution. Let's step through each scenario for rand5() % 4, for instance. 1%4 =1 2%4=2 3%4=3 4%4=0 5%4=1 . "1" is listed twice...meaning you're twice as likely to get a 1 than a 2 for instance.

An easy way to solve this problem is to find the least common multiple of 7 and 5, 35. You can then add rand5() seven times, and then you can modulo it with 7, knowing it will be uniform.

def rand7():
    return ( rand5() + rand5() +  rand5() +  rand5() +  rand5() +  rand5() +  rand5()  )  % 7

- Bruce Alan Romans Jr July 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Draw 2 random numbers from rand5()
2. Add them. Possible outcome will be contained in set [2,3,4,...,10]
3. Map [2,3,4,...,8] --> [1,2,3,...,7] and reject [9,10]

Addition of two random variables will give a uniform distribution in the interval [2,3,..,8].
We are using this section to draw numbers from [1...7] uniformly.

I hope it's correct theoretically.

- Sanjeev September 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Draw 2 random numbers from rand5()
2. Add them. Possible outcome will be contained in set [2,3,4,...,10]
3. Map [2,3,4,...,8] --> [1,2,3,...,7] and reject [9,10]

Addition of two random variables will give a uniform distribution in the interval [2,3,..,8].
We are using this section to draw numbers from [1...7] uniformly.

I hope it's correct theoretically.

- Sanjeev September 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Draw 2 random numbers from rand5()
2. Add them. Possible outcome will be contained in set [2,3,4,...,10]
3. Map [2,3,4,...,8] --> [1,2,3,...,7] and reject [9,10]

Addition of two random variables will give a uniform distribution in the interval [2,3,..,8].
We are using this section to draw numbers from [1...7] uniformly.

I hope it's correct theoretically.

- Sanjeev September 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Draw 2 random numbers from rand5()
2. Add them. Possible outcome will be contained in set [2,3,4,...,10]
3. Map [2,3,4,...,8] --> [1,2,3,...,7] and reject [9,10]

Addition of two random variables will give a uniform distribution in the interval [2,3,..,8].
We are using this section to draw numbers from [1...7] uniformly.

I hope it's correct theoretically.

- Sanjeev September 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

>>> from random import randint
>>> def f5(p1=5):
return randint(0,p1)
>>> def f7():
return f5(p1=7)

- sanjeev September 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

>>> from random import randint
>>> def f5(p1=5):
return randint(0,p1)

>>> def f7():
return f5(p1=7)

- sanjeev September 24, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you know rejection sampling...

import random
import matplotlib.pyplot as plt

def OneTwoThree():
    while True:
        x = random.randint(1,5)
        if x == 1 or x == 2 or x==3:
            return x
        
def rand7():
    while True:
        x = OneTwoThree()
        y = random.randint(1,5)
        if x == 1 or x == 2:
            return y
        if x == 3:
            if y == 1 or y == 2:
                return 6
            if y == 3 or y == 4:
                return 7
# Test
data = []
for i in range(100000):
    a = rand7()
    data.append(a)

plt.hist(data)
plt.show()

- Anonymous October 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import random
import matplotlib.pyplot as plt

def OneTwoThree():
    while True:
        x = random.randint(1,5)
        if x == 1 or x == 2 or x==3:
            return x
        
def rand7():
    while True:
        x = OneTwoThree()
        y = random.randint(1,5)
        if x == 1 or x == 2:
            return y
        if x == 3:
            if y == 1 or y == 2:
                return 6
            if y == 3 or y == 4:
                return 7
# Test
data = []
for i in range(100000):
    a = rand7()
    data.append(a)

plt.hist(data)
plt.show()

- Anonymous October 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def rand7():
  		return [1, 2 , 3, 4, 5, 6, 7][rand5() + rand5()*rand5()%3 - 1]

- Thanh Liem Tran January 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import numpy as np

def rand5():
return 1 + 4 * np.random.random(1)

def rand7():
return 1 + 6 * (rand5()-1)/4

- Anonymous January 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import numpy as np

def rand5():
return 1 + 4 * np.random.random(1)

def rand7():
return 1 + 6 * (rand5()-1)/4

- Andrew January 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from random import randint

def rand5():
return randint(1,5)

def rand7():
a1 = rand5()
a2 = rand5()
a3 = rand5()
a4 = rand5()
a5 = rand5()
a6 = rand5()
a7 = rand5()
print(a1,a2,a3,a4,a5,a6,a7)
print(a1+a2+a3+a4+a5+a6+a7)
return int((a1+a2+a3+a4+a5+a6+a7)/7.0)

print(rand7())

- Nerdsurd November 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from random import randint

def rand5():
  return randint(1,5)

def rand7():
  a1 =  rand5()
  a2 =  rand5()
  a3 =  rand5()
  a4 =  rand5()
  a5 =  rand5()
  a6 =  rand5()
  a7 =  rand5()
  return (a1+a2+a3+a4+a5+a6+a7)%7 +1

#QC : roughly equal probabilities
a1,a2,a3,a4,a5,a6,a7=0,0,0,0,0,0,0
for i in range(1,1002):
  x=rand7()
  if x == 1:
    a1 += 1
  elif x == 2:
    a2 += 1
  elif x == 3:
    a3 += 1
  elif x == 4:
    a4 += 1
  elif x == 5:
    a5 += 1
  elif x == 6:
    a6 += 1
  elif x == 7:
    a7 += 1

print(a1,a2,a3,a4,a5,a6,a7)

- nerdsurd November 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Use rand5() 7 times, take the sum and then mod 7. This gives us rand7()

- abc February 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

That is incorrect. There are 5^7 possible outcomes of this. That number is not divisible by 7 and thus you cannot redistribute 5^7 outcomes into 7 equally sized (and equally likely) buckets.

One way to do it:

int                                                                                                                                                                                 
rand7() {                                                                                                                                                                           
    int maxrnd = ((5 * 4 + 4) / 7) * 7;     // =21                                                                                                                                        
    int total = 0;                                                                                                                                                                  
    do {                                                                                                                                                                            
        total = rand5() * 5 + rand5();                                                                                                                                              
    } while (total >= maxrnd);                                                                                                                                                      
                                                                                                                                                                                    
    return total % 7;                                                                                                                                                               
}

rand5()*5+rand5() has uniform distribution in the [0;24] (or any subset of that). You want to have equal number of possibilities for each result in [0;6] so you keep the result if it is in the [0;20] range or try again if it is larger than 20.

- ml February 27, 2015 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

int rand7()
{
const int MAX = 5*5;
const int DIV = MAX/7;
int num = 5*(rand5()-1)+rand5()-1; // randomly distributed within 0:24
if(num < MAX)
return (num/div)+1;
else
return rand7();
}

- Zzz February 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

const int MAX = 5*5;
const int DIV = MAX/7;

int rand7()
{
    int num = (rand5()-1)*5+rand5()-1; // randomly distributed in 0:24
    return (num<MAX) ? (num/DIV) : rand7();
}

- ZZZ February 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Do rand(5) up to 7 times.

1. rand(5) -> if it is 1, return 1.
2. rand(5) -> if it is 1, return 2.
3. rand(5) -> if it is 1, return 3.
4. rand(5) -> if it is 1, return 4.
5. rand(5) -> if it is 1, return 5.
6. rand(5) -> if it is 1, return 6.
7. rand(5) -> if it is 1, return 7.

- phantomlord February 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That also does not work. In your case, the probability to return 1 is 1/5 (bad, should be 1/7). The probability to return 2 is 4/5 * 1/5 = 4/25, still not 1/7. In a similar way, the probabilities to return 3 through 7 are neither 1/7 nor equal to each other. Also you leave open the question what do we do if we don't get a 1 in all 7 tries?

- ml February 27, 2015 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Double rand7(){
Double a = rand5() +2.0

return a
}

- Sanket March 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

And how would rand7() ever return 1 or 2...?

- Colin March 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

double Rand7()
{
	return (rand5()/5)*7
}

- Nelson Perez February 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Did you understand the question in first place? your code will return only multiple of 7 like 0,7,14 etc which is nowhere close to the actual question asked

- Tota Ram Verma March 01, 2015 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

random5()+(random5())%3

- Saravanan Rathinavel February 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

random5()+(random5())%3

- Saravanan Rathinavel February 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

{random5()+(random5())%3}

- Saravanan Rathinavel February 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

rand7= (rand5()/5)*7

- Yogesh Magar February 27, 2015 | Flag Reply


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