Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

If you are familar with KM algoirthm (Solving Maximum Weighted Bipartite Matching), this question seems to be every easy.

generate three virtual schools with the same coordinates for each school, then apply the KM algoirthm for the given 9 school and 9 students. The weight is the distance from the student to the school.

- a solution based KM algorithm August 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks for your suggestion. Can you please elaborate it with an example...it would be helpful.

- Shwetank Gupta August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

en.wikipedia.org/wiki/Hungarian_algorithm
It is a greedy algorithm.

- jiangok2006 August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It's a problem in GAP. An iterative LP approach can be applied.

- ergul75 August 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This was in a phone interview? I hope you weren't asked to implement it.
More specifically, IMHO, this is mincost maxflow problem. Create one source node with directed link of capacity 3 and cost 0 to every school. Create links of capacity 1 from every school to every other student with cost = distance between corresponding school and the student. Then create links of capacity 1 from every student to a sink node and cost 0. The use mincost maxflow algorithm to maximize flow and minimize the cost. When the algorithm is done, it gives 3 links going out from each school to 3 students such that the cost is minimum. The logic and implementations are very complex.

- dc360 September 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Eugene Really? Then I am confused. This has to be more than bipartite matching. Afaik bipartite matching can't handle costs. If that's not the case, then can you please elaborate the proposed solution a little bit?

- dc360 September 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

+1

- dc360 September 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

here I am giving recursive approach (only basic idea)... there must be some DP solution for this question also...

int Min_dist(int *sc_dist, int p, int n1, int n2, int n3 , int sum_total)
{
        if(n1<3||n2<3||n3<3)
             {
             sum_total+=min(n1<0?min_sum(sc_dist, p-1,++n1,n2,n3, sum_total+sc[0][p]):INT_MAX,  min_sum(sc_dist, p-1,n1-1,++n2,n3, sum_total+sc[1][p]), min_sum(sc_dist, p-1,n1,n2,++n3 , sum_total+sc[2][p]));
             }
             return sum_total;

}

- novice August 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

here I am giving recursive approach (only basic idea)... there must be some DP solution for this question also...

int Min_dist(int *sc_dist, int p, int n1, int n2, int n3 , int sum_total)
{
if(n1<3||n2<3||n3<3)
{
sum_total+=min(n1<3?min_sum(sc_dist, p-1,++n1,n2,n3, sum_total+sc[0][p]):INT_MAX, n2<3?min_sum(sc_dist, p-1,n1-1,++n2,n3, sum_total+sc[1][p]):INT_MAX, n3<3?min_sum(sc_dist, p-1,n1,n2,++n3 , sum_total+sc[2][p]):INT_MAX);
}
return sum_total;

}

- novice August 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please explain with some example

- devendar August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about iterating through all the possible distributions of 9 students in 3 groups ?

- Cerberuz August 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Apply kruskal with deleting high weighted edges and considerering each student should have atleast one edge and each school three.

- LOL August 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

? The result must be three separate connected components.

- smparkes@smparkes.net August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I seriously doubt on Krushkal's approach:
Lets reduce the problem to 2 school each with one child for example ..
School coordinate S1: (0,0), S2: (3,0)
Student cord St1: (1,0) and St2: (-3,0)

Now u will get the result as (S1, St1) and (S2, St2) total distance = 7
On contrary (S1, St2) and (S2, St1) makes total distance = 5

One more thing :I forgot to mention in the question that the required time complexity should be lesser than O(n^3):

- Shwetank Gupta August 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Less than n^3 or no worse than n^3? Hungarian is n^3. Still think linear assignment is right. The graph is bipartite.

- smparkes@smparkes.net August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am just writing the details...anyone is free to provide solution of any time complexity...Anyways Solution is first...optimization afterwards

- Shwetank Gupta August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

DP solution:

Sum(i) - Minimum distance of all students upto i, satisfying the condition atmax 3 students in school.

Sum(i) = min { for all k = 0 to i-1, {sum(k) + for all school s = 1 to 3, d[k][s] if count[s] < 3}}

Order of the solution is O(n^2*S) - ( n- Number of students, S- number of school )

- Hello August 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

May not be an optimal solution,
9 students and 3 schools

Calculate distance between each student and each school

27 possiblities,
Sort them based on the distance for each school and pick the first 3 students for each school.

- Ramrott October 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Create three tasks for each school and this becomes linear assignment.

- smparkes@smparkes.net August 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Minimum spanning tree doesn't put a limit on inbound/outbound connections.

- Martin August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Minimum spanning tree optimized with Kruskal or Prim.

- surireale August 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The solution cannot be a tree, but 3 connected components each with 3 students and one school.

- Martin August 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This comment has been deleted.

- Administrator August 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It would be better if you please write psuedo code of your approach...

- Shwetank Gupta August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, oh God...

- Chengyun Zuo August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Could you please explain what will happen in case when A(1,0) , B(-1,0) ,C(0,1) and one stud cord is (0,0) . In this case , all the distance is same and suppose it is minimum and it has to be included in all the three schools to get the minimum for each school. What is suggested in this case?

- Anonymous August 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

school can be any wr randomly

- Anonymous August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Algo shld be lik below...
1.Create 3 virtual school
2.cretae 9 virtual students
3. use 3 array for fiding the distance of each student from 3 school use student id as index
4.when we got 3 array create 3 Binary tree.
5. find min in those 3 trees.& assign student to the school having minimum distance.
6.delete the node in other two trees.
7.Keep doing it untill all student are assigned...

We can have checks for max student as 3 in school in step 5 & can get 2nd min distance...

Plz comment...

- atul gupta August 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

euclidean minimum spanning tree

- saurabh August 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nope. Here we have additional constraints, like the number of students per school.

- eugene.yarovoi August 31, 2012 | Flag


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