Domanda di colloquio di Tinder

You have 2 unsorted arrays of integers that only differ by one element. Find that element in the most efficient way.

Risposte di colloquio

Anonimo

24 giu 2016

You could hash the elements or sum the arrays. Watch for things like overflows. Instead of summing the elements of the array you can xor the elements.

Anonimo

30 ago 2016

the question is vague. i am giving solution to two different problems as i understand it i) array A: [x1,....xk,..xn], B:[x1,..(xk missing)...xn] solution: simple running xor of both arrays with leave xk(that's why i think this is not the question) ii) A: [x1...xk...xn] V:[x1...xk2(instead of xk)...xn] solution: 1. run xor, that leaves xk(xor)xk2; find the first set of this xorsum bit from right -> this is one of the possible many locations where xk and xk2 differs 2; store this index position, lets say it's i. (formula to find last set bit is x & -x) 2. create int X=0, Y=0 which will hold final results(xk and xk2); for all elements in A and B, right shift them by i position and check if bitwise ANDing them with 1 returns true. Depending on the true/false outcome, xorsum them with X, Y Once you are done, X & Y will have xk & xk2