IBM Interview Question for Software Engineer / Developers






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

Hello,
does that mean for each execution it should give a different answer?

- upendra December 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry, but i don't understand your question. The function rand7 will generate random number between 1 and 7 inclusive, and this number is just random, not necessarily not generated before.

- Alzaadi shau neum December 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
main()
{
int myrand5();
printf("Rand %d\n",myrand5());
}
int myrand5()
{
int a;
return (-1*((int)(&a)%5)+1);
}

using this code you can find random no. from 1-5

----------------------------------------------------------------------------------
#include<stdio.h>
main()
{
int myrand5();
printf("Rand %d\n",(myrand5()+2)%7+1);
}
int myrand5()
{
int a;
return (-1*((int)(&a)%5)+1);
}

this will give you random no.s 1-7

- AKB December 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

its totally ridiculous as it wont generate 2 and 3..

- sk January 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="java" line="1" title="CodeMonkey10628" class="run-this">int rand15()
{
return (rand() % 5) + 1;
}

int rand17()
{
return ((rand15() + 7 + rand15()) % 7) + 1;
}

</pre><pre title="CodeMonkey10628" input="yes">
</pre>

- Anonymous December 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Well this answer is partially true. While it is correct that it will generate numbers between 1 and 7, the distribution of these numbers is not going be uniform.

If we try all possible combinations, the frequency of each number will be:

freq[1]=4
freq[2]=3
freq[3]=3
freq[4]=3
freq[5]=3
freq[6]=4
freq[7]=5

While 3 & 4 are expected, 5 is not expected. I think a better choice would be to replace

return ((rand15() + 7 + rand15()) % 7) + 1;

With

return (((rand15() - 1) * 5 + rand15() - 1) * 7) / 25;

The explanation of this is as follows:

It is possible that we will end up with 25 combinations, which we would like to map to 7 numbers. So we map to combinations to numbers linearly. In order to avoid getting 0 all the time, we multiply first, and then divide with the number of combinations. The distirbution becomes as follows:

freq[1]=4
freq[2]=4
freq[3]=3
freq[4]=4
freq[5]=3
freq[6]=4
freq[7]=3

Truly uniform. If 5 and 7 were not pairwise prime, the distribution would have been exactly equal for all numbers.

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

I`d replace rand17() with that:

int rand17(int k)
{
return ((rand15() + k * rand15()) % 7) + 1;
}

Analog of this algo is being used in hash tables when collisions take place.

So, main code to get 100 numbers from 1 to 7:

for(int i = 0; i < 100; ++i)
cout << rand17(i) << endl;

- Anonymous December 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

why not below one

rand7(){
return rand5()*7/5;
}

reasoning
for rand5()=0 rand7() will be 0 too.
for rand5()=5 rand7() will be 7
rest of rand5() ; rand7() should be distributed equally.

- Ashish December 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Bravo!!!

- Anonymous January 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How will we get 3 in this above equation. No way I suppose.

- Jay Patel July 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

x1=rand5();
x2=rand5();
x3 = (x1*10+x2)
finalRand = x3%7 + 1.

frequencies for each digit between 1 and 7 :: [4,3,3,4,4,3,4]

- Anonymous December 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

//This would be more uniform
x1=rand5()
x2=rand5()
...
x7=rand5()
sum = x1+...x7
finalRand = sum%7 + 1

- blueskin December 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i am agree with you.

- sk June 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

simple...make a random func that generates a rand no b/w 0 n 2 and it with rand5...

- hi December 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

and add it with rand5... sry for the mistake..

- hi December 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

simple but not good. why? because the numbers generated using this algo are not equally distributed, it is more as a gaussian curve.
how? assume 'randHi' generates between 0 and 2, then as per ur algorithm all the possible numbers are 1+0,2+0,3+0,4+0,5+0,1+1,2+1,3+1,4+1,5+1,1+2,2+2,3+2,4+2,5+2 --> [1,2,3,4,5,2,3,4,5,6,3,4,5,6,7]
frequencies of numbers between 1 to 7 --> [1,2,3,3,3,2,1]
probabilities of numbers between 1 to 7 -->[1/15,2/15,3/15,3/15,3/15,2/15,1/15]
as you can see this is a bell curve i.e. the probability of generating 1 or 7 is lower than generating 2 or 6. which is lower than generating 3 or 4 or 5.
we want our function to generate all numbers between 1 to 7 with equal probabilities. i.e. 1/k

- not hi December 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int rand17()
{
   return rand15() + abs(rand15()-3);
}

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

It is impossible to write a random generator to give you 1-7 number with 1/7 probability using 1/5 generator.

- fiddler.g January 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- write a function Rand12 which return 1 or 2 with equal prob (50/50 chance) using the 1 to 5 generator you have. Just ignore any 3, 4, or 5 until you get 1 or 2.

- draw a number 1 - 5, call it X, then if Rand12 is 2, add 5 to X. Now X is uniform 1 - 10. Keep doing this step until your 1 - 10 number is not 8, 9, or 10.

Not too efficient but it works.

- this is simple January 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Did you try to calculate the time complexity of this algorithm, or the real probability for each number from 1-7? :)

- fiddler.g January 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

rand5()+ rand5()%4 -1;

min value=1+1-1=1
max value=5+3-1=7

- keshav February 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

looks good

- helen May 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

u did mistake in calculation..
rand5()%4 can have minimum value 0 not 1, so in that case total answer becomes 0 for min value.

- sk June 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Lets represent 7 in base 5. It will require 2 bits. So, call Rand5() for selecting the zeroth bit and rand5() for calling 1st bit. If the result is greater than 7, discard it. Note that the first bit doesn't depend on the second bit. So, probably of each bit is random, hence the final number is also random. I think this approach works for all these sort of examples.

Please correct me if I am wrong.

- Messi February 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

http: // stackoverflow .com/ questions/137783/expand-a-random-range-from-1-5-to-1-7

- Anonymous February 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in the main program write the below code.. outside the main(global variable)

static int a[5]={1,2,3,4,5};

then call this function rand1_5()..each time it will give a number between 1 to 5.

int rand1_5()
{
static int i=0;
i++;
if(i==4)
i--;

return arr[i];

}

- asetech February 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

editing ..

int rand1_5()
{
static int i=0;
i++;
if(i==4)
i--;

if(i==0)
i++;

return arr[i];

}

- asetech February 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is impossible for me not to understand why won't this be a good solution:

rand15()-1 -random btw. 0 and 4
(rand15()-1)/2.0 -random btw. 0 and 2
((rand15()-1)/2.0)*3 -random btw. 0 and 6
1+((rand15()-1)/2.0)*3 -random btw. 1 and 7

- Adrian February 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

rand5() generates numbers between 1-5
so probability is 3/5 and 2/5

- avico81 January 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. rand5() will give result from 0 to 5.
2. Probablity of getting an even number or an odd number (assuming rand5() is perfect) will be .5 each
3. 7 when converted into binary has 3 set bits.
4. call rand5() 3 times if result is even then set the corresponding bit otherwise make that bit 0.

Basically here I am trying to generate all the bits with equal probablity

psuedo code:

int multiplicationFactor = 1;
int rand7Number = 0;
for(int i = 0 ; i < 3 ; i++)
{
      int j = rand5();
      rand7Number += (j%2) *  multiplicationFactor ;
      multiplicationFactor *= 2;
}
return rand7Number ;

- Paras March 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it could be genOne2Seven() = ( genOne2Five() -1) * (7-1) / (5-1) +1;

- judith March 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

( ( rand15() + rand15() ) % 7 ) +1

i think this would work ...

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

for rand7, each number between 1 and 7 has roughly 15% probability to be selected.
for rand5, each number between 1 and 5 has 20% probability to be selected.
What about we use rand5() to control the probabilities for 1-7 to be selected?
Like for rand5, the probability of a number is not being selected is 80%.
code:
int a = rand5(), b = rand5();
if (a!=1 && b != 1) return 1;
else if (a!=1 && b != 2) return 2;
else if (a!=1 && b != 3) return 3;
else if (a!=1 && b != 4) return 4;
else if (a!=1 && b != 5) return 5;
else if (a!=2 && b != 1) return 6;
else if (a!=3 && b != 1) return 7;

- rw7026 May 12, 2013 | 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