Practo Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

do this toss() func 3 times
let XXX is result where X = H, T
ignore case of where all X are H or all are T
now the sequence will be
HHH = 1/8
HHT = 1/8
HTH = 1/8
HTT = 1/8
THH = 1/8
THT = 1/8
TTH = 1/8
TTT = 1/8
all with probability 1/8 now if HHH or TTT comes toss again for 3 times until different event occurs.

take H as 0 and T = 1
so the sequence will be
001= 1
010 = 2
011 = 3
100 = 4
101 = 5
110 = 6

all with equal probability (though we are skipping operations sometimes still)

- Bharat Kumar Arya January 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good explanation

- jilinxie1988@gmail.com January 16, 2015 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

public static int tossDice(){
	  int bit1 = toss();
	  int bit2 = toss();
	  int bit3 = toss();

	  if(bit1==0&&bit2 == 0&&bit3==0){
		  return 1;
	  }else if(bit1 == 0&&bit2 == 0&&bit3==1){
		  return 2;
	  }else if(bit1 == 0&&bit2 == 1&&bit3==0){
		  return 3;
	  }
	  else if(bit1 == 0&&bit2 == 1&&bit3==1){
		  return 4;
	  }else if(bit1 == 1&&bit2 == 0&&bit3==0){
		  return 5;
	  }else if(bit1 == 1&&bit2 == 0&&bit3==1){
		  return 6;
	  }else{	 
		  return tossDice();
	  }
  };

- varun.me15 January 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static int tossDice(){
int bit1 = toss();
int bit2 = toss();
int bit3 = toss();

if( bit1 == 0 && bit2 == 0 && bit3 == 1 ){
return 1;
}else if ( bit1 == 0 && bit2 == 1 && bit3 == 0){
return 2;
}else if( bit1 == 0 && bit2 == 1 && bit3 == 1){
return 3;
}
else if( bit1 == 1 && bit2 == 0 && bit3 == 0 ){
return 4;
}else if( bit1 == 1 && bit2 == 0 && bit3 == 1){
return 5;
}else if( bit1 == 1 && bit2 == 1 && bit3 == 0){
return 6;
}else{
return tossDice();
}
};

- nish.007 July 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static int tossDice(){
	  int bit1 = toss();
	  int bit2 = toss();
	  int bit3 = toss();

	  if(bit1 == 0&&bit2 == 0&&bit3==1){
		  return 1;
	  }else if(bit1 == 0&&bit2 == 1&&bit3==0){
		  return 2;
	  }
	  else if(bit1 == 0&&bit2 == 1&&bit3==1){
		  return 3;
	  }else if(bit1 == 1&&bit2 == 0&&bit3==0){
		  return 4;
	  }else if(bit1 == 1&&bit2 == 0&&bit3==1){
		  return 5;
	  }else if(bit1==1&&bit2 == 1&&bit3==0){
		  return 6;
	  }else 	 
		  return tossDice();
	  }
  };

- nish.007 July 20, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Toss coin 3 times (6 can be represented by 3 bits). If coin is heads set bit to 1, if coin is tails set bit to 0. If you get three heads in a row or three tails in a row [111 (7) or 000 (0)], start over. Otherwise convert the 3 bits into an integer (it will be between 1 and 6) and return that.

- akyker20 January 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I propose the following solution: (i) in general, for a random generator 0 - N-1, you toss the coin n = ceil(log2(N)) times and concatenate the result. The result can be viewed as a random binary number. Hence you have a random generator between 0 and M= (2^n-1) (ii) Now the problem remains that it could be that N<M. In this case generate a random number x and if it is out of range, throw it away, and generate the new one until you get the right one.

A sample code is shown below:

public int randomDice() {
	return random(6)+1;
}
// random generator 0 - N-1
public int random(int N) {
	if (N <=1)			return 0;
	int n = Math.ceil(Math.log2(N));
	int x;

	while(true) {
		x = 0;
		for (int k=0; k<n; k++)
			x += Math.pow(2, k)*(toss()?1:0);
		if (x < N) 	break;
	}
	
	return x;
}

- autoboli January 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The idea: if we toss the coin 3 times, we can generate 8 values. If we remove 2 of them we only have 6 values.

Pseudo code:

int toss () {} //return 0 or 1

int generateRandomValue ()
{
	int sum = 0;
	while (!(sum <= 6 && sum >= 1)) {
		for (int i = 0; i < 3; i++) {
			sum += toss () * math.pow (2, i)
		}	
	}
}

You can see full Python code at:

http://www.codeskulptor.org/#user39_nrdKC6WYMt_1.py

- Vinh January 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

All of this soltions are not feasible.
The algorithm can (with zero probability) run infinintly.

- David January 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

All of this soltions are not feasible.
The algorithm can (with zero probability) run infinintly.

- David January 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't understand, what do you mean?

- Vinh January 23, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote
we have have simple while for reclaculating invalid value. {{{ int get_dice(){ return pow(2,0)*toss()+pow(2,1)*toss()+pow(2,2)*toss(); } while(1){ int value=get_dice(); if(value>=1&&value<=6) break; } - Anonymous November 19, 2020 | 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