Domanda di colloquio di Hulu

Given array with random integers from negative infinity to infinity, Find the number that only appears once in this array (all the other numbers appear twice).

Risposte di colloquio

Anonimo

29 ott 2010

XOR the values in the array. XOR is commutative and associative, so all the numbers that appear twice will get zeroed out. The last number will get XOR'd with 0 which will give us the number itself.

12

Anonimo

21 gen 2011

You can also sort the array, then traverse it with a (for i = 0; i < array.length; i +=2). If array[index] == array[index + 1] then it's a duplicate.

1

Anonimo

24 ago 2011

first loop: just use one hashset of integers and put all the numbers in the array into the hashset but before you put them in the hashset, remove the number if it already exists in the hashset 2nd loop: then loop through the hash set again with a 2nd for loop and check if the set contains the ith element in the array, the set should only have one element in it and that is the number that appeared once. one hashset only, and two for loops, O(2n) (assuming insertion/deletion O(1))

Anonimo

18 dic 2012

Since its from infinity to infinity, assuming that we have a large heap. 1. Create a "Set"; call it a "unique" set. 2. As we go though a number, check the "Set" to see if it already exists in the set. a. if the number is not in the set, simply put it in the set. (as this is a candiate for unique number) b. if the number is already present in the set, remove the number from the set as it is dupe. With this algoritm you should have "unique" number at any given time. Another way is to populate a map, with the number as key and "count" as value.

Anonimo

12 feb 2011

That's a dumb question. You can't add up or sort an infinite array.