trophygeek
BAN USER- 0of 0 votes
AnswersWrite a function that takes an array of numbers and returns the maximum and minimum values.
- trophygeek in United States
Give BigO for runtime.
(This is a basic coding question. There are no real tricks or shortcuts.)| Report Duplicate | Flag | PURGE
Google Software Engineer Coding
There's a lot to like about this answer. Max & min use the first value in the array, the function returns both values.
You can skip arr[0] and start at arr[1].
Pretty good answer.
It is an actual function and it returns two values.
- What happens when an empty array is passed? []
- How does the caller know which value is max and which is min in the returned Tuple? Can you think of a way to make it more clear?
- How would you do this if it input was an array of floats?
You can use fewer compares than comparing each value twice (once to max and once to min). Technically, I think the function is O(n) but the *comparisons* can be O(3n/2). This is because Big O simplifies (3n/2) down to just n for conversation purposes.
There is also a better way than using a MAX_INT/MIN_INT constants... You'll need to find it to solve for the "make a function that works for floats".
[OP]
Follow up:
- Part of the problem is correctly returning two values from a function.
- Question was posed as an array of "numbers", everyone assumed int, but what if it was an array of float or doubles.
- Make sure to handle input of an empty array [].
This is an old question. Don't think it's used anymore.
- trophygeek February 25, 2015However, it actually has a lot of subtleties. The beauty of it is that it seems trivial and there is a wide skill range of correct answers. Every programmer should be able to solve it in the simplest case; however, advanced programmers can go deeper.
I've only seen a couple of people get the advanced 3N/2 solution correctly. And nobody has offered a C++ template or C# generics version yet. (The original question is posed as "array of numbers".)