Facebook Interview Question for Senior Software Development Engineers


Country: United States
Interview Type: Phone Interview




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

A small fix in the above code..

class Solution {
	public:
		string sumStrings(string a, string b) {
			int i = a.size()-1;
			int j = b.size()-1;
			
			int carry = 0;
			string res = "";
			while(i>=0 || j>=0 || carry==1) {
				carry += i>=0 ? (a[i--] - '0') : 0;
				carry += j>=0 ? (b[j--] - '0') : 0;
				
				res = char(carry%10 + '0') + res;
				carry = carry/10;
			}
			return res;
		}
}

- datta.pm July 08, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include<string.h>
#include<math.h>
#define MAX 10000

int main(void) {
	int i = 0, k, len1, len2, sum, tmp, l1, l2, leng, a[MAX], b[MAX];
	char string1[MAX], string2[MAX], ans[MAX];
	printf("enter a number : ");
	scanf("%s",&string1);
	printf("enter another number : ");
	scanf("%s",&string2);
	
	//printf("\nThe 1st number is : ");
	while(string1[i]){
			a[i] = (int)string1[i]-48;
			//printf("%d",a[i]);
			i++;
	}
	
	i = 0;
	//printf("\nThe 2nd number is : ");
	while(string2[i]){
			b[i] = (int)string2[i]-48;
			//printf("%d",b[i]);
			i++;
	}
	
	len1 = strlen(string1), len2 = strlen(string2);
	printf("\nlengths are respectively : %d and %d", len1, len2);
	
	if(len1 >= len2)
		leng = len1;
	else
		leng = len2;
	
	for(i = 0, l1 = len1-1, l2 = len2-1; i < leng; i++, l1--, l2--){
		ans[i] = a[l1] + b[l2];
	}
	
	
	for(k = 0; k <= leng; k++){
		sum = ans[k];
		tmp = sum/10;
		ans[k] = sum%10;
		ans[k+1] += tmp;
	}

	for(i = leng; i>= 0;i--)
    {
        if(ans[i] > 0)
            break;
    }
    
	printf("\nThe summation is : ");
	for(k = i; k >= 0; k--){
		printf("%d",ans[k]);
	}
	return 0;
}

- The_Chaotic_Butterfly July 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Solution {
	public:
		string sumStrings(string a, string b) {
			int i = a.size()-1;
			int j = b.size()-1;
			
			int carry = 0;
			string res = "";
			while(i>=0 || j>=0 || carry==1) {
				carry += i>=0 ? a[i--] : 0;
				carry += j>=0 ? b[j--] : 0;
				
				res = char(carry%10 + '0') + res;
				carry = carry/10;
			}
			return res;
		}
}

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

class Solution {
	public:
		string sumStrings(string a, string b) {
			int i = a.size()-1;
			int j = b.size()-1;
			
			int carry = 0;
			string res = "";
			while(i>=0 || j>=0 || carry==1) {
				carry += i>=0 ? a[i--] : 0;
				carry += j>=0 ? b[j--] : 0;
				
				res = char(carry%10 + '0') + res;
				carry = carry/10;
			}
			return res;
		}
}

- Somnath July 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My answer in Java would be like this

private static String addNumbers(String no1, String no2)  
    {
        String sum = "";
        int bal = 0;
        int max = no1.length() > no2.length() ? no1.length() : no2.length();
        for (int count = 1; count <= max || bal > 0; count++) {
            if (count <= max){  
                if (no1.length() - count >= 0)                  
                    bal += (no1.charAt(no1.length() - count) - '0');
                if (no2.length() - count >= 0)                      
                    bal += (no2.charAt(no2.length() - count) - '0');
            }
            sum = "" + (bal % 10) + sum;
            bal /= 10;
        }
        return sum;
    }

- PeyarTheriyaa July 08, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void GetSumOfLargeNumbers(string value1, string value2)
{
List<char> summation = new List<char>();
value1 = value1.PadLeft(15, '0');
value2 = value2.PadLeft(15, '0');
int carryOver = 0;
for (int i = value1.Length - 1; i > 0; i--)
{

int value = carryOver + Convert.ToInt32(value1[i].ToString()) + Convert.ToInt32(value2[i].ToString());
if (value > 9)
{
carryOver = Convert.ToInt32(value.ToString()[0]);
summation.Add(value.ToString()[1]);
}
else
summation.Add(value.ToString()[0]);
}
summation.Reverse();
Console.WriteLine(string.Join("", summation));
}

- Sathish July 13, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void GetSumOfLargeNumbers(string value1, string value2)
{
List<char> summation = new List<char>();
value1 = value1.PadLeft(15, '0');
value2 = value2.PadLeft(15, '0');
int carryOver = 0;
for (int i = value1.Length - 1; i > 0; i--)
{

int value = carryOver + Convert.ToInt32(value1[i].ToString()) + Convert.ToInt32(value2[i].ToString());
if (value > 9)
{
carryOver = Convert.ToInt32(value.ToString()[0]);
summation.Add(value.ToString()[1]);
}
else
summation.Add(value.ToString()[0]);
}

summation.Reverse();

Console.WriteLine(string.Join("", summation));
}

- Sathish July 13, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ruby

-> (a, b) {
    zero = '0'.ord
    [a, b].
        sort_by(&:length).
        reverse.
        map(&:bytes).
        map(&:reverse).
        reduce(&:zip).
        reduce(result: [], overflow: 0) { |prev, (adig, bdig)|
            ov, digs = ((adig || zero) + (bdig || zero) - 2 * zero + prev[:overflow]).divmod 10
            {overflow: ov, result: prev[:result] << digs}
        }.
        tap { |prev| prev[:result] << 1 if prev[:overflow] == 1 }[:result].
        reverse.join
}

- saverio.trioni July 13, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

golang:

package main

import (
"fmt"
"math"
)

func main() {
fmt.Println(sum("1989", "39900"))
fmt.Println(1989+39900)
}


func sum(a, b string) int64 {
aSize := len(a)
bSize := len(b)
maxLen := math.Max(float64(aSize), float64(bSize))
result := 0
tmp := 0
x := 1
for i := 0; i < int(maxLen); i++ {
digitA := 0
digitB := 0
if aSize - i >= 1 {
digitA = int(a[aSize - i - 1]-'0')
}
if bSize - i >= 1 {
digitB = int(b[bSize - i - 1]-'0')
}
if digitA > 9 || digitA < 0 || digitB > 9 || digitB < 0 {
return -1
}
sum := digitA + digitB + tmp
tmp = sum/10
sum %= 10
result += sum * x
x *= 10
}
if tmp == 0 {
return int64(result)
}
return int64(result+x)
}

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

golang:

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(sum("1989", "39900"))
	fmt.Println(1989+39900)
}


func sum(a, b string) int64 {
	aSize := len(a)
	bSize := len(b)
	maxLen := math.Max(float64(aSize), float64(bSize))
	result := 0
	tmp := 0
	x := 1
	for i := 0; i < int(maxLen); i++ {
		digitA := 0
		digitB := 0
		if aSize - i >= 1 {
			digitA = int(a[aSize - i - 1]-'0')
		}
		if bSize - i >= 1 {
			digitB = int(b[bSize - i - 1]-'0')
		}
		if digitA > 9 || digitA < 0 || digitB > 9 || digitB < 0 {
			return -1
		}
		sum := digitA + digitB + tmp
		tmp = sum/10
		sum %= 10
		result += sum * x
		x *= 10
	}
	if tmp == 0 {
		return int64(result)
	}
	return int64(result+x)
}

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

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(sum("1989", "39900"))
	fmt.Println(1989+39900)
}


func sum(a, b string) int64 {
	aSize := len(a)
	bSize := len(b)
	maxLen := math.Max(float64(aSize), float64(bSize))
	result := 0
	tmp := 0
	x := 1
	for i := 0; i < int(maxLen); i++ {
		digitA := 0
		digitB := 0
		if aSize - i >= 1 {
			digitA = int(a[aSize - i - 1]-'0')
		}
		if bSize - i >= 1 {
			digitB = int(b[bSize - i - 1]-'0')
		}
		if digitA > 9 || digitA < 0 || digitB > 9 || digitB < 0 {
			return -1
		}
		sum := digitA + digitB + tmp
		tmp = sum/10
		sum %= 10
		result += sum * x
		x *= 10
	}
	if tmp == 0 {
		return int64(result)
	}
	return int64(result+x)
}

- Anonymous July 15, 2018 | 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