Google Interview Question

Design an algorithm, which can record the largest number in an ever-upgrading sequence.

Interview Answers

Anonymous

Jun 4, 2012

Why sort the sequence? Just keep track of the largest number and compare it to the next one.

Anonymous

Mar 9, 2016

I this this question is NOT how to find largest number in a sequence, but this sequence can be updated all the time for any slots. So, in this case, you cannot use any static data structure, but consider a dynamic one to solve.

Anonymous

Jul 20, 2019

There are a few questions to ask before you assume you know what you are coding for. In the most strict sense a sequence according to mathematics is a set of objects or values that have been ordered in a sequential fashion. SO... if that is the case then you would simply note that the last value is the largest in the sequence. However I do not think that was the intent of the question. In that case, we need to make sure we ask what exactly is the requirement? Are we provided an unsorted list / unsorted sequence? Do we need to store all of the values or only need to store the largest value and can negate all others? How is the sequence upgraded? Are all of the values provided again with the upgraded sequence or are we only provided the new values to consider? Once you have determined what exactly the requirement is then we will know what exactly we are supposed to code. Are we simply doing a comparison and swap for the larger value or do we need to incorporate a map / hash and sort to return the largest value? In interviews, remember, it is not always that you provide the correct answer, but how you determine and come up with the correct answer. Don't assume, confirm.

Anonymous

Sep 10, 2010

Depending on the size of the sequence, how about: 1. If the sequence is small, just scan the sequence and return the largest number 2. If the sequence is large, use a max-heap (implemented as priority_queue in C++STL for example). After each element add, add the element also to the end of the heap and do 'max-HEAPIFY' on that node to maintain the max-heap property. After each element removal, move the last element of the heap to the removed node and do 'max-heapify" on that node also maintain the max-heap property. The largest number of the whole sequence is always the first/top element. 3. If the sequence is large, can also use a set (RB tree, std::set) to store all the elements, return the max element from the set. I suspect #2 is what they expect, but just to make a point, if the sequence is really small, #1 can be better although it is dumb,

1

Anonymous

Mar 8, 2012

since its only trying to find the largest one, why not just sort the seqence?