## Interview Question

Data Scientist Interview

-San Francisco, CA

Meta## Given an list A of objects and another list B which is identical to A except that one element is removed, find that removed element.

## Interview Answers

19 Answers

All these supposed answers are missing the point, and this question isn't even worded correctly. It should be lists of NUMBERS, not "objects". Anyway, the question is asking how you figure out the number that is missing from list B, which is identical to list A except one number is missing. Before getting into the coding, think about it logically - how would you find this? The answer of course is to sum all the numbers in A, sum all the numbers in B, subtract the sum of B from the sum of A, and that gives you the number.

Anonymous on

select b.element from b left join a on b.element = a.element where a.element is null

Anonymous on

In Python: (just numbers) def rem_elem_num(listA,listB): sumA = 0 sumB = 0 for i in listA: sumA += i for j in listB: sumB += j return sumA-sumB (general) def rem_elem(listA, listB): dictB = {} for j in listB: dictB[j] = None for i in listA: if i not in dictB: return i

BurmesePython on

How about this in python, will this work? x = set(listA)-set(listB) print(x)

Anonymous on

missing_letters = [] for letter in A: if letter in B: pass else: missing_letters.append(letter) print (missing_letters)

Teni on

In R: removed_element <- A[which(!A %in% B)] removed_element

Anonymous on

Select * from A except Select * from B

Anonymous on

I think it is a coding in algorithm rather than SQL query. So here is my take: def ret_miss(A, B): k = len(A) if k == 2: if A[1] == B[0]: return A[0] elif A[0] == B[0]: return A[1] n = k/2 print A[n], B[n] if A[n] == B[n]: A= A[n:] B=B[n:] else: A=A[:n+1] B=B[:n+1] print A,B return ret_miss(A,B) This works nicely actually.

Chaiwala on

# python code def missing_obj(original_lst, new_lst): for x in new_lst: original_lst.remove(x) return original_lst

JPC on

In SQL: SELECT A.object FROM A LEFT JOIN B ON A.object = B.object WHERE B.object IS NULL;

Morgan on

Careful, there could be a repeated object that's being removed. i.e. A = [3, 4, 5, 6, 5] B = [3, 4, 6, 5] This is how I would do it on Python (works for numbers and strings) def missingval(lA, lB): a = sorted(lA) b = sorted(lB) c = None for i in range(len(b)): if a[i] != b[i]: c = a[i] break if c is None: c = a[-1] print(c, "was removed from list A")

Anonymous on

SQL: select a.list as a, b.list as b from ListA as a full join ListB as b on a.list = b.list where a.list eq '' OR b.list eq "" ;

Anonymous on

XOR all elements

Anonymous on

A = [1,2,3,4,5,6,7,8] B = [1,2,3,4,5,6,8] [i for i in A if i not in B]

Anonymous on

find the sum of the two list and subtract. ans = sum(a) - sum(b) where a and b are list of numbers.

Anonymous on

Depends on the kind of elements in the lists. If they're numbers, sum(A) minus sum(B) will give the missing element. If they're characters/strings, just dump the elements of A into a dictionary and check each element in B for existence in A.

dopey on

[i for i in A if i not in [j for j in B]]

Beena on

Python: sum(A)-sum(B)

Anonymous on

two ways to do it using sql: 1. select * from A where not in (select * from B) -- assuming you know what element you're looking for 2. select * from (select * from A UNION select * from B) having count(element) = 1 -- again assuming you know the element

Anonymous on