Pergunta de entrevista da empresa Meta

Write a function that takes in two sorted lists and outputs a sorted list that is their union.

Respostas da entrevista

Sigiloso

31 de jul. de 2016

Second part of merge sort. Don't answer with sort(a), etc. Anyone can do that... def merge(A, B): i=0 j=0 sorted_list = [] while i < len(A) and j < len(B): if A[i] <= B[j]: sorted_list.append(A[i]) i += 1 else: sorted_list.append(B[j]) j += 1 if i < len(A): sorted_list.extend(A[i:]) elif j < len(B): sorted_list.extend(B[j:]) return sorted_list

3

Sigiloso

10 de ago. de 2014

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.

3

Sigiloso

25 de ago. de 2016

I assumed that we can not use any "sort" function and we want it with linear time. so here it is: def my_sort(list_a, list_b): if len(list_a) ==0: return list_b elif len(list_b) ==0: return list_a else: if list_a[-1] > list_b[-1]: return( my_sort(list_a[0:-1], list_b) + [list_a.pop(-1)]) else: return(my_sort(list_a,list_b[:-1]) + [list_b.pop(-1)])

Sigiloso

13 de nov. de 2013

google merge sort

Sigiloso

5 de fev. de 2018

In SQL SELECT List1 FROM Table1 UNION SELECT List2 FROM Table2 ORDER BY List1, List2;

Sigiloso

10 de ago. de 2014

Oops, check/write a termination condition

1

Sigiloso

26 de set. de 2014

On Python, you could do: from sets import Set def merge_sort(a,b): return sorted( Set(a).union(Set(b)) )

Sigiloso

10 de jun. de 2015

def sorted_union(list1, list2): union=set(list1).union(set(list2)) sorted_union=sorted(list(union)) return sorted_union

Sigiloso

6 de set. de 2013

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

Sigiloso

14 de jun. de 2013

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

2