Facebook Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

Looking for interview experience sharing and coaching?
Visit A++ Coding Bootcamp at aonecode.com.

Given by experienced engineers/interviewers from FB, Google and Uber,
our ONE TO ONE courses cover everything in an interview including
latest interview questions sorted by companies,
SYSTEM DESIGN Courses (highly recommended for people interviewing with FLAG)
ALGORITHMS (conquer DP, Graph, Greedy and other advanced algo problems),
and mock interviews.

Our students got offers from G, U, FB, Amz, Yahoo and other top companies after a few weeks of training.

Feel free to email us aonecoding@gmail.com with any questions. Thanks!

public static int divide(int dividend, int divisor) {

        if(divisor == 0) throw new RuntimeException();

        int sign = 1;
        //figure out the sign of the result
        if((dividend < 0 && divisor > 0)
                || (dividend > 0 && divisor < 0)) {

            sign = -1;

        }

        if(dividend < 0) dividend = -dividend;
        if(divisor < 0) divisor = -divisor;

        int n = 1; //get the range of result [n, 2n], where n is doubled every round

        while(dividend > (n << 1) * divisor) {

            n <<= 1;

        }

        //now it's known that the result is between [n, 2n]
        //so in the dividend has a sum of more than n and less than 2n divisors in total
        //to figure out exactly how many divisors sum up to the dividend,
        // break down the problem to result = n + divide(dividend - n * divisor, divisor)
        // next round the dividend becomes (dividend - n * divisor) with n added to the result.
        // proceed to figure out the range [x, 2x] of result for the new dividend, where x can be n/2, n/4, n/8...
        // whenever the x is found, add x to the result and deduce x * divisor from the dividend
        // util n is 0 or dividend is 0

        //If written in math, the formula looks like dividend = ([T/F]* n + [T/F]* n/2 + [T/F]* n/ 4 + [T/F]* n/ 8...) * divisor
        //The quotient will be ([T/F]* n + [T/F]* n/2 + [T/F]* n/ 4 + [T/F]* n/ 8...), [T/F] depends on (dividend - n * divisor) >= 0
        int result = 0;

        while(n > 0 && dividend > 0) {

            if(dividend - n * divisor >= 0) {

                dividend -= n * divisor;
                result += n;

            }

            n >>= 1;

        }

        return result * sign;

    }

- aonecoding June 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void Divide(int a, int b, out int quotient, out int reminder)
        {
            quotient = 0;
            reminder = 0;
            int aCopy = a;
            while (aCopy >= b)
            {
                quotient++;
                aCopy -= b;
            }

            reminder = aCopy;
        }

- Anonymous June 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void Divide(int a, int b, out int quotient, out int reminder)
        {
            quotient = 0;
            reminder = 0;
            int aCopy = a;
            while (aCopy >= b)
            {
                quotient++;
                aCopy -= b;
            }

            reminder = aCopy;
        }

- ylkiranmayi June 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As the function returns an int we suppose we are trimming the decimals and we just only return the int part, e.g. 3/2 = 1.5, result = 1

now we have that a/b = c thus giving us a=c*b, then we need to find a value where c times b is equal to a, so we iterate over the possibles values of c

function divide(a,b) {
	i=0;
	while(b*i<=a){
            i++;
	}
	console.log(i-1);
}

- jaylu June 26, 2017 | 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