Amazon Interview Question for SDE-2s


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
4
of 4 vote

Implement a min heap of 1 million numbers

- Anonymous April 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Y min heap... y not max heap.

- sjain April 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 votes

should be using a min heap. A min heap put the minimal value among those 1M max numbers on the top. Whenever a new number "a" is read from the stream, compare it with the top of the heap "b". If a < b, just throw a. Otherwise, replace b with a, heapify.

- xiaoxipan April 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@deep0mal
what do you mean by 'track' ?
it could mean: count occurrence, find smallest/largest/median/average ...

- VJ April 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is find largest 1 million numbers in the stream.

- deep0mal April 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void findLargest(int* const dataSet,const int dataSetSize,const int numLargest)
{
    vector<int> heap;
    for(int dataIndex = 0; dataIndex < dataSetSize; ++dataIndex)
    {
        const int nextInt = dataSet[dataIndex];
        if(heap.size() < numLargest)
        {
            heap.push_back(nextInt);
            push_heap(heap.begin(),heap.end(),greater<int>());
        }
        else
        {
            int heapMin = heap.front();
            if(nextInt > heapMin)
            {
                pop_heap(heap.begin(),heap.end(),greater<int>());
                heap.pop_back();
                
                heap.push_back(nextInt);
                push_heap(heap.begin(),heap.end(),greater<int>());
            }
        }
    }
    
    for(vector<int>::const_iterator it = heap.begin(); it != heap.end(); ++it)
    {
        printf("%d ",*it);
    }
    printf("\n");
}

- jay@fallcreek.com April 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just implement a min-heap with 1 million of size.
In any time in the heap you will have the first million of max numbers

private class Heap
		{
			private int[] array;

			private int currentSize = 0;

			public Heap(int size)
			{
				this.array = new int[size];
				this.currentSize = 0;
			}

			public bool IsFull
			{
				get
				{
					return this.currentSize == array.Length;
				}
			}

			private void Swap(int first, int second)
			{
				int tmp = array[first];
				array[first] = array[second];
				array[second] = tmp;
			}

			public bool Insert(int value)
			{
				if (!IsFull)
				{
					array[currentSize++] = value;
					HeapifyUp(currentSize - 1);
					return true;
				}

				return false;
			}

			public void HeapifyUp(int curr)
			{
				while (curr != 0)
				{
					int parent = curr % 2 == 0 ? (curr >> 1) - 1 : curr >> 1;

					if (array[curr] < array[parent])
					{
						Swap(curr, parent);
						curr = parent;
					}
					else
					{
						return;
					}
				}
			}

			public int GetRoot()
			{
				if (currentSize > 0)
				{
					return array[0];
				}

				throw new Exception("Heap is empty");
			}

			public void UpdateRoot(int value)
			{
				if (currentSize > 0)
				{
					array[0] = value;
					HeapifyDown(0);
				}
			}

			public void HeapifyDown(int curr)
			{
				while (true)
				{
					int left = (curr << 1) + 1;
					int right = (curr << 1) + 2;

					if (left >= currentSize)
					{
						break;
					}

					int optimal = left;

					if (right < currentSize && array[right] < array[left])
					{
						optimal = right;
					}

					if (array[curr] > array[optimal])
					{
						Swap(curr, optimal);
						curr = optimal;
					}
					else
					{
						break;
					}
				}
			}
		}

		public void ProcessNumbers(IEnumerable<int> numbers)
		{
			Heap h = new Heap(5);

			foreach (int number in numbers)
			{
				if (!h.IsFull)
				{
					h.Insert(number);
				}
				else
				{
					int min = h.GetRoot(); // it will be the minimum value of heap
					if (number > min)
					{
						h.UpdateRoot(number);
					}
				}
			}
		}

- ZuZu April 19, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More