## Facebook Interview Question for Software Engineer / Developers

Country: United States
Interview Type: Phone Interview

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

``````private static String negaBinary(int x) {
StringBuilder sb = new StringBuilder();
while (x != 0) {
int rem = x % -2;
x = x / -2;
if (rem < 0) {
rem += 2;
x += 1;
}
sb.append(rem);
}
return sb.reverse().toString();
}``````

It is very similar to transforming into 2-base but tricky case is when reminder is negative.
we just have to convert negative reminder into non-negative:
a=b*c + r, so if r <0 then we can do a=b*c + r +b-b=b*(c+1) + (r-b)
in our case b=-2

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

nice

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

Can you explain more what do you mean ?

Comment hidden because of low score. Click to expand.
2
of 2 votes

(-2)^0 = 1
(-2)^1 = -2
(-2)^2 = 4
(-2)^3 = -8
(-2)^4 = 16
(-2)^5 = -32 and so on

i. e. for 2 = 1 * 4 + 1 * (-2) + 0 * 1 => 110
-15 = 1 * -32 + 1 * 16 + 0 * -8 + 0 * 4 + 0 * -2 + 1 * 1 => 110001

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

Is it not simple as take compliment and then add one.

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

may be it is better to use a XOR rather than add one?

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

No, the is the simple part. The hard part of the question is sum two numbers in (-2)-base :-)

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

may be it is better to use a XOR rather than add one?

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

``````#!/usr/bin/env python3

def negabase(number, base):
if 0 == number:
return '0'

digits = list()
while 0 != number:
number, remainder = divmod(number, base)

if remainder < 0:
# N == b*q + (b - b) + r == b*(q + 1) + (r - b)
number, remainder = number + 1, remainder -	base

digits.append(str(remainder))

return ''.join(digits[::-1]) if len(digits) else '0'

def solution(number):
return negabase(number, -2)

assert solution(-15) =='110001'
assert solution(10) ==  '11110'
assert solution(-5) == '1111'``````

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

package dir;

class result {

int reminder;
int result;

public result(int reminder, int result) {
this.reminder = reminder;
this.result = result;
}

public result() {
}

}

public class NewClass {

public NewClass() {
int num = 13;
int div = -2;
StringBuilder sb=new StringBuilder();
while (Math.abs(num) >0) {
result r = divide(num, div);
num = r.result;
//print(r);
sb.append(r.reminder);
}
sb.reverse();
System.out.println(sb);
}

void print(result r) {
System.out.println("result " + r.result + " reminder = " + r.reminder);
}

result divide(int num, int div) {
result r = new result();
//r.result = num % div;
int count = 0;
int reminder = 0;
if (num == 0 || div == 0) {
r.reminder = 0;
r.result = 0;
}

if (num > 0) {
if (div > 0) {
//later
} else {
while (num >= -div) {
num = num + div;
count++;
}

r.result = -count;
}
} else {
if (div > 0) {
//later
} else {
// both are negative
while (num < 0) {
num = num - div;
count++;
}

r.result = count;
}
}

r.reminder = num;
return r;
}

public static void main(String[] args) {
new NewClass();
}
}

Comment hidden because of low score. Click to expand.
-2
of 2 vote

1's complement and then 2's complement.

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