Pergunta de entrevista da empresa Google

how would you find maximum element in an array of numbers.

Respostas da entrevista

Sigiloso

23 de fev. de 2010

Honestly, something like this is probably the best (assuming an unsorted array): int getBest(int[] nums){ int max; for(int i = 0; i max) max = curr; } return max; } You have to look at every element once, because if you skip an element you can't be sure it isn't the biggest. The lowest bound we can do is O(n). However, two loops makes this an O(n^2) problem, doing much more work without helping us solve the problem.

6

Sigiloso

3 de ago. de 2010

sort the list. quicksort gives average of O(nlogn) complexity. return the last element in the list O(1) complexity.

Sigiloso

13 de fev. de 2010

2 loops and comparing each element with all other as its not sorted array.