Amazon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Phone Interview
//Here is the c++ version.
#include <iostream>
#include <list>
#include <iomanip>
#include <cmath>
#include <stdlib.h>
using namespace std;
/* Heap Sort Methods*/
void heapSorting(double arr[], int);
void heapify(double arr[], int);
void siftDown(double arr[],int, int);
int main(){
list<int> x_coordinate; //x co-ordinate list
list<int> y_coordinate;//y co-ordinate list
int i,j;
int count=0;
cout<<"Please do not enter comma between co-ordninate"<<endl;
cout<<"Enter 99 99 to exit"<<endl;
for(;i != 99, j !=99;)
{
cout <<"Enter co-ordinate "<<count+1 << " = ";
cin>> i >> j;
cout<<endl;
if(i == 99 || j ==99) {;}
else{
x_coordinate.push_back(i);
y_coordinate.push_back(j);
count++;
}
}
//iterators
list <int>::iterator x;
list <int>::iterator y;
int size = x_coordinate.size();
double arr[size];
int k =0;
//calculating square root
// the value is storedin array arr
for(x = x_coordinate.begin(),y = y_coordinate.begin(); x != x_coordinate.end() && y != y_coordinate.end();
x++,y++)
{
arr[k] = sqrt((*x * *x) + (*y * *y));
k++;
}
//calling heapsorting
heapSorting(arr, size);
int nearest_point;
cout << "Enter k (number of nearest points from origin) >> ";
cin >> nearest_point;
cout<<endl;
for(int t = 0; t < nearest_point; t++ )
{
cout<< fixed << setprecision(2) << arr[t] << " ";
}
cout<<endl;
return 0;
//iterators
list <int>::iterator x;
list <int>::iterator y;
int size = x_coordinate.size();
double arr[size];
int k =0;
//calculating square root
// the value is storedin array arr
for(x = x_coordinate.begin(),y = y_coordinate.begin(); x != x_coordinate.end() && y != y_coordinate.end();
x++,y++)
{
arr[k] = sqrt((*x * *x) + (*y * *y));
k++;
}
//calling heapsorting
heapSorting(arr, size);
int nearest_point;
cout << "Enter k (number of nearest points from origin) >> ";
cin >> nearest_point;
cout<<endl;
for(int t = 0; t < nearest_point; t++ )
{
cout<< fixed << setprecision(2) << arr[t] << " ";
}
cout<<endl;
return 0;
//iterators
list <int>::iterator x;
list <int>::iterator y;
int size = x_coordinate.size();
double arr[size];
int k =0;
//calculating square root
// the value is storedin array arr
for(x = x_coordinate.begin(),y = y_coordinate.begin(); x != x_coordinate.end() && y != y_coordinate.end();
x++,y++)
{
arr[k] = sqrt((*x * *x) + (*y * *y));
k++;
}
//calling heapsorting
heapSorting(arr, size);
int nearest_point;
cout << "Enter k (number of nearest points from origin) >> ";
cin >> nearest_point;
cout<<endl;
for(int t = 0; t < nearest_point; t++ )
{
cout<< fixed << setprecision(2) << arr[t] << " ";
}
cout<<endl;
return 0;
}
//----------------------------------------
void heapSorting(double arr[], int size){
heapify(arr, size);
int last = size - 1;
while(last > 0)
{
//swap the root(max value) of heap with last element
swap(arr[last], arr[0]);
last -= 1;
//maintaining max-heap order
siftDown(arr,0, last);
}
}
//-----------------------------------------
void heapify(double arr[], int size){
//index as a last parent node
int start = (size -1)/2;
while (start >=0){
siftDown(arr, start, size-1);
start -=1;
}
}
//------------------------------------------------------
void siftDown(double arr[],int first, int last)
{
int root = first;
int child;
while(root * 2 + 1 <= last)
{
child= root * 2 + 1;
int to_swap = root;
if(arr[to_swap] < arr[child])
{
to_swap = child;
}
if(child+1 <= last && arr[to_swap] < arr[child+1])
{
to_swap = child+1;
}
if(to_swap!= root )
{
swap(arr[root], arr[to_swap]);
root = to_swap;
}
else
{
;
}
}
}
compute distance for each point from origin i.e sqrt(x^2 + y^2), you can ignore sqrt to reduce complexity burden if you want. that would not affect.
- abhishekatuw February 22, 2012then we can use max-heap if k is small enough.
else we can do partition based method similar to quick sort, to separate out k smallest distance from the rest.