Amazon Interview Question


Country: India




Comment hidden because of low score. Click to expand.
6
of 10 vote

use size bounded min-heap
if the incoming number is greater than the root of the min-heap, replace the root with the new number and min-heapify the heap

- dgbfs November 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

n=3, the numbers in stream are 10,9,8,7,6,5... max of last 3 elements is 7 .. if we maintain min heap on 10,9,8 => u never insert rest of elements ..
If u maintain max heap also, ans will be wrong ..

- bharat November 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

U just need to keep track of a counter .. and that will solve the problem...

- Bevan November 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it seems to be the correct one. we can maintain a fixed size heap say N. and then compare the smallest value i.e. arr[0] with new number if it is greater then arr[0] then replace and heapify.
shall come up with code tomorrow.

- kb arora December 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//function to find maximum number in the input stream
# include<stdio.h>
# include<conio.h>
//this function will evaluate maximum number entered in a continuous stream of numbers
int find_max()
{
char ch;
int num, max=-5000;
printf("start entering numbers in the stream\n");
do
{
printf("enter your number");
scanf("%d",&num);
if(num>max)
max=num;
printf("max till now is %d\n",max);
printf("do u want to continue if yes press y and if no then press n\n");

fflush(stdin);
scanf("%c",&ch);

}while(ch!='n');//end of do whle loop
return 0;
}//end of function
int main()
{
find_max();

return 0;
}//end of main function

- Anonymous November 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//function to find maximum number in the input stream
# include<stdio.h>
# include<conio.h>
//this function will evaluate maximum number entered in a continuous stream of numbers
int find_max()
{
char ch;
int num, max=-5000;
printf("start entering numbers in the stream\n");
do
{
printf("enter your number");
scanf("%d",&num);
if(num>max)
max=num;
printf("max till now is %d\n",max);
printf("do u want to continue if yes press y and if no then press n\n");

fflush(stdin);
scanf("%c",&ch);

}while(ch!='n');//end of do whle loop
return 0;
}//end of function
int main()
{
find_max();

return 0;
}//end of main function

- Anonymous November 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

#define N 10

void heapify(int arr[], int ind) {
	if(ind <= N) {
		int smaller = ind;
		int left = ind<<1;
		int right = left+1;
		if(left <= N && arr[left] < arr[smaller])
			smaller = left;
		if(right <= N && arr[right] < arr[smaller]) 
			smaller = right;
		if(smaller != ind) {
			int temp = arr[ind];
			arr[ind] = arr[smaller];
			arr[smaller] = temp;
			heapify(arr, smaller);
		}
	}
}
	

main() {
	int count = 0, i;
	int arr[N+1];

	while(count < 50) {
		int num;
		scanf("%d", &num);
		count++;

		if(count > N) {
			if(num > arr[1]) {
				arr[1] = num;
				heapify(arr, 1);
			}

			for(i = 1; i <= N; ++i) {
				printf("%3d", arr[i]);
			}
			printf("\n");
		}
		else
			arr[count] = num;
	}

	return 0;
}

- Srikant Aggarwal November 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Finds Sliding window maximum ..
void maxSlidingWindow(int A[], int n, int w, int B[]) {
deque<int> Q;
for (int i = 0; i < w; i++) {
while (!Q.empty() && A[i] >= A[Q.back()])
Q.pop_back();
Q.push_back(i);
}
for (int i = w; i < n; i++) {
B[i-w] = A[Q.front()];
while (!Q.empty() && A[i] >= A[Q.back()])
Q.pop_back();
while (!Q.empty() && Q.front() <= i-w)
Q.pop_front();
Q.push_back(i);
}
B[n-w] = A[Q.front()];
}

- bharat November 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will do in O(n) time and O(w) space complexity.

- bharat November 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <vector>
using namespace std;

class NMax {
	int n_;
	vector<int> min_heap_;
	
    void heapify(int root) {
		int left = 2 * root + 1;
		int right = 2 * root + 2;
		int minV = min_heap_[root];
		int toHeapifyNext = -1;
		if(left < min_heap_.size() && min_heap_[left] < minV) {
			minV = min_heap_[left];
			toHeapifyNext = left;
		}
		if(right < min_heap_.size() && min_heap_[right] < minV) {
			minV = min_heap_[right];
			toHeapifyNext = right;
		}
		if(toHeapifyNext != -1) {
			swap(min_heap_[root], min_heap_[toHeapifyNext]);
			heapify(toHeapifyNext);
		}
		
	}
public:
	NMax& operator() (int num) {
		if(min_heap_.size() < n_) {
			min_heap_.push_back(num);
			heapify(int(min_heap_.size() / 2) - 1);
		} else {
			if(num > min_heap_[0]) {
				min_heap_[0] = num;
				heapify(0);
			}
		}
		return *this;
	}
	vector<int> max() { return min_heap_;}
	NMax(int n):n_(n){};
};
int main() {
	NMax stream(3);
	stream(97)(1)(2)(3)(4)(6)(1)(34)(9);
	auto max = stream.max();
	for(auto n : max) {
		cout << n << endl;
	}
	return 0;
}
Output:
9
97
34

- Mhret November 27, 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