Amazon Interview Question
Country: India
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 ..
//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
//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
#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;
}
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()];
}
#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
use size bounded min-heap
- dgbfs November 09, 2012if the incoming number is greater than the root of the min-heap, replace the root with the new number and min-heapify the heap