Facebook Interview Question: Write a function that takes i... | Glassdoor

## Interview Question

Software Engineer Intern Interview(Student Candidate) Palo Alto, CA

# Write a function that takes in two binary strings and

returns their sum (also a binary string).

This post has been removed.

0

#include
#include

using namespace std;

string findBinSum(string a, string b) {
/*
Iterate through strings and find integer values.
Find sum. Convert sum back to binary string.
*/
int sizeA = a.size();
int sizeB = b.size();
int valA =0;
int valB =0;
for (int i=sizeA-1; i &gt;= 0;i--)
{
if (a[i] == '1') {
valA += (int)pow((double)2,(double)(sizeA-1-i));
}
}
for (int k=sizeB-1; k &gt;= 0;k--)
{
if (b[k] == '1') {
valB += (int)pow((double)2,(double)(sizeB-1-k));
}
}
cout =0; j--) {
numUsed = (int)pow((double)2,(double)j);
if ( sum-numUsed &gt;= 0) {
sum -= numUsed;
}
else {
}
}
}

int main(){
string binary1 = "1011";
string binary2 = "1011";

string answer = findBinSum(binary1,binary2);
cout &lt;&lt; "sum: " &lt;&lt; answer &lt;&lt; endl;
}

Tim on Dec 21, 2011