Facebook Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

Looking for interview experience sharing and coaching?

Visit aonecode.com for private lessons by FB, Google and Uber engineers

ONE TO ONE real-time coaching offers

SYSTEM DESIGN Courses (highly recommended for candidates for FLAG & U)
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algos & Clean Coding),
latest interview questions sorted by companies,
mock interviews.

Our students got hired from G, U, FB, Amazon, LinkedIn and other top tier companies after 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 Exception();
        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 July 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Why make it more complex, this is clean, neat... and works.

def div_wo(a,b){
  panic( b == 0 , 'Terrible parameter!')
  printf('%d/%d%n', a,b)
  sign_change = false  
  if ( a < 0  ){
    sign_change = true   
    a = -a 
  } 
  if ( b < 0 ){
    sign_change ^= true
    b = -b
  } 
  res = 0 
  while ( a >= b ){
     res += 1
     a -= b 
  } 
  sign_change ? -res : res 
}
println( div_wo(0,1) )
println( div_wo(1,1) )
println( div_wo(1,2) )
println( div_wo(2,1) )
println( div_wo(2,-1) )
println( div_wo(-2,-1) )
println( div_wo(-2,1) )
println( div_wo(3,2) )

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

a divided by b:

// a/b
        int a = 41;
        int b = 5;
        int rem = a;
        int ans =0;
        while(rem>=b){
            rem-=b;
            ans++;
        }
        System.out.println("ans:"+ans+"|rem="+rem);

- krupen.ghetiya July 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@NoOne: it's then just linear runtime O(dividend/divisor) whereas aone did actually place a logarithm in there (O(lg(dividend/divisor)).

1. terminology:
quotient = dividend / divisor

2. assumptions, constraints:
a) integer values (including signs)
b) positive and negative
c) since python, integers are 'unbound' no need to check overflow, but with Java or c++ you should check overflow, actually!

3. solution
1) turn the signs around until only positive integers remain
2) now do a 'high school division' on paper kind of thing in base 2, e.g.
1001001101 : 101 = (1 + 4 + 8 + 64 + 512) / 5
1111234567
1) 1001 / 101 = 1 remainder 100
2) 1000 / 101 = 1 remainder 11
3) 110 / 101 = 1 remainder 1
4) 11 / 101 = 0 remainder 11
5) 111 / 101 = 1 remainder 10
6) 100 / 101 = 0 remainder 100
7) 1001 / 100 = 1 remainder 100
= 1110101 Remainder 100

def division(dividend, divisor):
  # division by zero case
  if divisor == 0: raise ZeroDivisionError()

  # turn signs to get rid of -dividend, -divisor
  if dividend < 0: return -division(-dividend, divisor)  
  if divisor < 0: return -division(dividend, -divisor)

  # sdiv = divisor * 2^n, sdiv > divisor and sdiv <= divisor * 2
  sdiv = divisor
  while sdiv < dividend:
    sdiv <<= 1 

  # remove divisior * 2^i from dividend until divisor * 2^0
  # therefore the quotient is multiplied by two in each iteration
  quotient = 0
  while sdiv >= divisor:
    quotient <<= 1 
    if dividend >= sdiv:
      dividend -= sdiv
      quotient |= 1
    sdiv >>= 1

  return quotient

print(division(981, 12) == 81)
print(division(11, 12) == 0)
print(division(13, 13) == 1)
print(division(99191, 15) == 6612)
print(division(-1, 1) == -1)
print(division(1, -2) == 0)
print(division(2, -1) == -2)
print(division(-9, -3) == 3)

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

inp1 divided by inp2:

int inp1 = 32;
        int inp2 = -8;
        int a = Math.abs(inp1);
        int b = Math.abs(inp2);
        int rem = a;
        int ans =0;
        if(b>0 && a>0){
            while(rem>=b){
                rem-=b;
                ans++;
            }
        }
        if((inp1<0 && inp2>0) ||(inp1>0 && inp2<0) ){
            ans=-ans;
        }
        System.out.println("ans:"+ans);

- krupen.ghetiya July 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void divideNumbers(int a , int b){
		int result = 0;
		int remainder = 0;
		
		while(a-b > 0){
			result++;
			remainder = a-b;
			a = remainder;
		}
		
		System.out.println("Result = "+ result);
		System.out.println("Remainder = "+ remainder);
	}

- Mayank Jain August 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public static void main(String args[]) throws Exception {
		System.out.println(divideWHTSign(300, 30));
	}

	public static int divideWHTSign(int a, int b) {
		int quotient = 0;
		while (a >= b) {
			quotient++;
			a -= b;
		}
		return quotient;
	}

- koustav.adorable July 23, 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