Pergunta de entrevista da empresa Amazon

Given array of integers, find the first pair add up to 10. Analize your solution.

Respostas da entrevista

Sigiloso

16 de jul. de 2010

using hashmap, loop through the array once, and populate the map with 10-array[i] as key and i as value. loop again and check the map if map.get(array[i]) != null. if the map has a value the value is the index of the other number. break out of the loop. (part that I didn't answer is about 5,5 case that you have to check value from the map must not be the same as the current index.) This is O(2n) = O(n), linear solution.

Sigiloso

2 de mar. de 2012

#define SIZE 8 int tab[SIZE]; int sum; for(i=0; i

Sigiloso

24 de jul. de 2010

using hasmap O(n) solution: for every element i in array if arr[i] <= 10 if (hashmap.containskey(10-arr[i])) // no. corresponding to reqd pair is present. return arr[i] else hashmap.put(arr[i],1)