Amazon Interview Question for SDE1s


Country: United States




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

Use a min heap of size 100. Insert the first 100 elements of the list into the heap. From the 101st element, check if the current element in the list is greater than the min element in the heap. If yes, delete the min element and insert the current element into the min heap. Repeat this until the list is exhausted and in the end, the top 100 elements will be present in the heap.

- Murali Mohan January 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about using external sorting instead of heap sort?

External Sort: en.wikipedia.org/wiki/External_sorting

Time complexity: stackoverflow.com/questions/10359661/time-complexity-cost-of-external-merge-sort

- coder January 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

It is not a heap sort it is using of min heap. That approach has O(n lg 100) time complexity, but using merge sort has O(n lg n). So first approach is better.

- Ivan January 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

insert first k element in BST klog k

now for n-k elements

element is smaller than smallest element in bst then delete it and insert curr element

logk finding smallest element + log k insert newly element in bst = 2 log k(worst case)

so overall its takes

klogk + (n-k)logk (find smallest) + (n-k)logk
= klogk +2*nlogk -2*klogk = 2*nlogk + klogk = nlogk

- satveer January 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Why not just use a hash table? Always insert each number into the table, no matter what. If it's already in there, it doesn't matter, since you are replacing a number with itself. Then return the 100 highest entries in the table as your new hash table, so you don't run out of memory.

We don't need to sort it, since the hash does that. We don't care if we have a duplicate, since each bucket only holds one item. Finally, we don't care where it falls in relation to anything else, since we always return the top 100 entries.

- gdg January 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if all numbers don't fit into memory than also you're hashtable doesn't. Additionally you need to actively look for the numbers to find the top 100. As a hashtable is not sorting them in any particular order

- langeolli January 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution with heap implemented in C++

#include <iostream>
#include <fstream>
#include <queue>

int main() {
	std::priority_queue<int, std::vector<int>, std::greater<int>> queue;
	std::vector<int> vec{10, 20, 30, 40, 50};

	for(auto value: vec) {
		queue.push(value);

		if(queue.size() > 2)
			queue.pop();		
	}

	while(queue.size() > 0) { 
		std::cout << queue.top() << std::endl;
		queue.pop();
	}

	return 0;
}

- Felipe Cerqueira January 12, 2014 | 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