Google Interview Question

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

Interview Answers

Anonymous

Feb 23, 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

Anonymous

Aug 3, 2010

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

Anonymous

Feb 13, 2010

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