Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

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.
then 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.

- abhishekatuw February 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why arent the actual code provided to any of the solutions?

- peacepanda007 February 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

compute the distance and insert into k mean heap O(nlogk)

- Sanjay Kumar February 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be thought as a top-k problem. Find all n distances and save them into a priority queue, then extract first k elements in O(nlogn) time-complexity.

- Yue March 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Which data structure is better for this ? Priority Queue vs Heap ?

- VA April 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

//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
		{
			;
		}
	}
}

- ba127004 February 22, 2012 | 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