Facebook Interview Question for Software Engineer / Developers


Country: -




Comment hidden because of low score. Click to expand.
14
of 18 vote

the easiest way i feel...
int myrand(int x)
{
int y=rand()%100;
if(y<x)
return 1;
return 0;
}

- Amit July 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Incorrect.

As stated on xhxtxtxpx:x/x/xwxwxwx.xcxpxlxuxsxpxlxuxsx.xcxoxmx/xreference/cstdlib/RAND_MAX/ (remove the x's)

"This value is library-dependent, but is guaranteed to be at least 32767 on any standard library implementation."

If your library uses 32767, then each number from 0 to 67 has a 328/32768 chance of being set to your y, and everything else from 68 to 99 only has a 327/32768 chance.

- Jonathan Nolife November 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class Probability {

	public static void main(String[] args) {
		int x = 5;
		// write a function to generate true with a probability of 65/100
		for (int i = 0; i < 10; i++) {
			System.out.println(generateTrue(x));
		}

	}

	public static boolean generateTrue(int x) {
		double random = Math.random() * 100;
		if (random < x) {
			return true;
		} else {
			return false;
		}
	}

}

- Amritaansh Verma October 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

In java you can do it as so:

public static int getRandom(Random r, int x)
	{
		return ((r.nextInt(100)) < x) ? 0 : 1;		
	}

There are 0-64 (65) chances that the generator will end up returning 1, and 65-99 (35) chances it will return 0.

- Anonymous July 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

should be "1 : 0" but otherwise ok ;)

- Safi November 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

/*
Given a number x, less than 100. How will you generate true with probability x/100. So if x = 65, how will you generate true with probability 65/100. You can represent true by 1 and false by 0.
Developed BY:Suman Roy
Emil : email.suman.roy@gmail.com
*/

#include<iostream>
#include<stdlib.h>
#include<ctime>
using namespace std;
int main(){
 int x, temp;
  std::cout<<"Enter x\n";
  std::cin>>x;
 int count1=0;
 int count2=0;
  for ( int i=0;i<100;i++){
  if ( x>100 ) {
              std::cout<<"X is greater than 100 \n";
              exit(EXIT_SUCCESS);
              }
	temp=rand() % 100 + 1;
         if ( temp <= x ){
            count1++;
             std::cout<<"true="<<1<<std::endl;
           }
          else{
		count2++;
            std::cout<<"False="<<0<<std::endl;
              }
        }
 std::cout<<"true occured="<<count1 <<"\n"<<"False occured="<<count2<<std::endl;
return 0;
}

- Anonymous July 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

developerslife.ru/3405

- klec July 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<cmath>
#include<cstdlib>
#include<time.h>
using namespace std;
int main(void)
{
	srand(time(NULL));
	double prob = ((double)rand())/RAND_MAX;	//preserving the assumed uniform-random-nature of output
	int input;
	cin >> input;
	if (prob < input*0.01)	cout << "True" << endl;
	else		cout << "Flase" <<  endl;
	return 0;
}

- RandomCoder July 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Fisher Yates Shuffle algo can be used using an array of size 100 ,defining the probability that every x end up in a position above x after shuffling(randomizing).

- Sumit Monga February 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Step 1. Take an array of size 100.
Step 2. Fill it with 'x' number of 1s.
Step 3. Apply Fisher Yates shuffle on it.
Step 4. Pick an element at random. rand()%100 .

- capt_caveman March 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

here is the solution test it with System.err.println(Arrays.toString(generateProbality(65, 100)));

public static int[] generateProbality(int n, int max) {
		if (n > max)
			return null;
		int[] result = new int[max];
		for (int i = 0; i < result.length; i++) {
			result[i] = 0;
		}

		Random random = new Random();

		for (int i = 0; i < n; i++) {
			int index = random.nextInt(max);
			if (result[index] == 0) {
				result[index] = 1;
			} else
				i--;
		}
		return result;
	}

- Melad.Raouf August 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Take an array,'a', of size 100 and fill it with 0's and 1's at random. Take care that number of 1's is exactly 'x'. Now randomly pick a number between 1 and 100,say 'i'. Output a[i].

Drawback: Every time the value of 'x' changes array has to be modified.

- hak23 July 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

JS:

return Math.ceil(Math.random()*100) <= x ? true : false

- nickinuse July 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

seems like

return Math.floor(Math.random()*100) < x

would be better

- gmetrofun September 18, 2013 | Flag


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