Facebook Interview Question for SDE1s


Country: United States




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

To compute all possible pairs instead of using brute force which would mean all the possible pairs I have used as a lower bound 10 ** (digits of the sum - 1) - 10 ** (digits of the sum - 2) and sum - (10 ** (digits of the sum - 2) - 10 ** (digits of the sum - 3)). I am still sure there are better approximations

def get_num_digits(num):
    digits = []
    while num >= 10:
        digits.append(num % 10)
        num /= 10
    digits.append(num)
    return digits
                  
def filter_pairs(x):
    n1 = get_num_digits(x[0])
    n2 = get_num_digits(x[1])
    for x in n2:
        if x not in n1:
            return False
        n1.remove(x)
    return True
    
def getNumbers(s):
    num_len = len(get_num_digits(s))
    pairs = []
    min_num = 10 ** (num_len - 1) - 10 ** (num_len - 2)
    max_num = s - (10 ** (num_len - 2) - 10 ** (num_len - 3))
    print min_num, max_num
    for x in xrange(min_num, max_num + 1):
        pairs.append((x, s - x))
    return filter(filter_pairs, pairs)

- Fernando May 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There are many ways to do it, here is one.

/* 
The regex is :
n2 = (\d)?x(\d)?y...(\d)?
under |string_n1| = |string_n2| + 1  
*/
def get_number( sum_value ){
  list ( [ 1: sum_value /2 + 1 ]  ) -> {
      n2 = $.o 
      n1 = sum_value - n2 
      // when the digits are not one up 
      s_n1 = str(n1)
      s_n2 = str(n2)
      continue ( size(s_n2) + 1 != size(s_n1) )
      decorated_n2 = fold( s_n2.value , '(\d)?' ) -> { $.p + $.o + '(\d)?' }
      continue( s_n1 !~ str(decorated_n2) )
      [ n1, n2 ]  
  }
}
ns = get_number ( int(@ARGS[0]) )
println(ns)

- NoOne May 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void findPairs(int num)
{
for(int i=num/100*100; i<num-(10);i++)
{

if(String.valueOf(i).contains(String.valueOf((num-i)/10)) && String.valueOf(i).contains(String.valueOf((num-i)%10)))
{
System.out.print(i+":"+ (num-i));
System.out.print("\n");
}


}

}

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

public static void findPairs(int num)
{
for(int i=num/100*100; i<num-(10);i++)
{

if(String.valueOf(i).contains(String.valueOf((num-i)/10)) && String.valueOf(i).contains(String.valueOf((num-i)%10)))
{
System.out.print(i+":"+ (num-i));
System.out.print("\n");
}


}

}

- unnikrishnankavungalanat May 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My python solution.

# Brute force solution less than O(n^2))
# O(n*m)
# where m
# m =~ 10^(len(n)-1) - 10^(len(n)-2)

import math

def check(n1,n2):
	n1 = list(str(n1))
	n2 = list(str(n2))
	if len(n1)-1 != len(n2):
		return False
	for char in n2:
		if char not in n1:
			return False
		else:
			n1.remove(char)
	return True

n = 124
val = []

r = int(math.pow(10,len(str(n))-1))
a = int(math.pow(10,len(str(n))-2))

for n1 in xrange(n):
	for n2 in xrange(a,r):
		if n1+n2 == n:
			if check(n1,n2):
				val.append((n1,n2))
print val

- nunziomeli5 May 24, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- majia168 Can you tell us how many times you are interviewed by FB?

- Anonymous July 02, 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