Facebook

www.facebook.com

Interview Question

Data Scientist Interview Menlo Park, CA

Write a function that takes in two sorted lists and outputs

  a sorted list that is their union.
Tags:
sql, product, science, analytics, python, data
Answer

Interview Answer

6 Answers

0

f(a,b) { return sort(unique(a,b)) }

Anonymous on Jun 14, 2013
0

def sortedUnion(list1,list2):
    list3 = [x for x in list1 if x in list2]
    return sorted(list(set(list3)))

Anonymous on Sep 6, 2013
0

google merge sort

Anonymous on Nov 13, 2013
2

write 2 helpers: 1) INSERT(A, b) = put element b within A in the sort order
2) DEL(A, a) = delete element a from A

Then do this recursion:
f(A,B) : if max(A) <= min(B) return [A B]
            else { B = INSERT(B, max(a));
                      A = DEL(A, max(a);
                      f(A,B);
                     }

something like that. try coding and testing. I haven't.

TheProf on Aug 9, 2014
0

Oops, check/write a termination condition

TheProf on Aug 9, 2014
0

On Python, you could do:

from sets import Set

def merge_sort(a,b):
    return sorted( Set(a).union(Set(b)) )

wp on Sep 26, 2014

Add Answers or Comments

To comment on this, Sign In or Sign Up.