Amazon Interview Question


Country: India
Interview Type: In-Person




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

can anyone explain question please. i could not understand this que even??
thanx

- Anonymous April 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Finding Hamiltonian path in a tournament graph.

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

This is true in the most general case, and Hamiltonian path is NP-complete. There are no known sub-exponential time algorithms that detect Hamiltonian paths exactly.

However, if the orderings are transitive -- in other words, victories accurately reflect the skill of the players in such a way that if A beat B and B beat C, then A beat C for all triplets A, B, C, then this problem simplifies to a topological sort, an algorithm that can be done in time proportional to the number of edges. There are N(N-1)/2 edges if there are N players, and so the time would be O(N^2).

- eugene.yarovoi April 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hamiltonian path in tournament graph has a P time solution. See the wiki.

- Anonymous April 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Interesting. I had not heard of tournament graphs before and just ignored the term because I thought you used it only because we were discussing a tournament. I see now that it is fairly trivial to prove (by induction) that every tournament graph has a Hamiltonian path, and that an O(N^2) algorithm for this task can be obtained directly from this proof.

- eugene.yarovoi April 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@eugene if we do it the topological sorting way how is the complexity O(n^2). We should choose a combination from all possible outputs (from topology sort) that has all nodes which makes it O(n^3)

- Anonymous April 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@eugene if we do it the topological sorting way how is the complexity O(n^2). We should choose a combination from all possible outputs (from topology sort) that has all nodes which makes it O(n^3)

- Anonymous April 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

it looks like the varient of insertion sort .. so i guess its complexity would be n^2.

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

One Aprroach :- I believe it is an insertion sort so the time complexity is O(n^2).

Second Approach:- If transitive behaviour is given then it becomes a sorting algorithm where we have to sort in ascending order of number of matches won. Time Complexity O(nlogn).

- Free Bird April 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think, sorting does not solve the problem.


A,B,C,D 4 players

A own 2 , B own 2 , c own 1 D own 1 matches

sorting gives ABCD answer but this answer may not be right.

B might have won to C

- siva.sai.2020 April 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ siva I see your point.
Then i will stick to my first approach of Insertion Sort.

- Free Bird April 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Uh...insertion sort? That doesn't make any sense to me. Can you explain?

- eugene.yarovoi April 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yup we can insert any one of the candidates in an array then when we take another candidate and check with current one and place him accordingly. Then we take the next one and place him in this array by finding a suitable position for this candidate and so on.

- Free Bird April 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Free Bird: surprisingly, it so happens that your solution is correct. However, no offense or anything, but I'm inclined to think you just got very lucky. It so happens that there's always a suitable position for the next competitor to be placed, but did you actually prove this before you decided this was a legitimate solution? Without some reasoning about it, it's not immediately clear that every ordered sequence of K candidates has a place into which the K+1st candidate can be inserted without otherwise rearranging the sequence.

- eugene.yarovoi April 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can also think that in the form of quick sort and i as pivot

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

It is topological sort.
Also, in this problem is given that graph constructed out of player statistic, does not contain directed cycle (meaning a lost to b, b lost to c, c lost to a, for such case there is ordering possible)

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

Apparently there always exists an ordering that can be discovered in P-time even if there are cycles. I just found out about this too. Read the wiki article on tournament graphs.

- eugene.yarovoi April 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Lets consider each node to be a player. It also keeps track of player which it has defeated. i.e. A -> B means A has defeated B. Then perform a topological sorting or topological ordering on that graph. You should be able to get the players in the order what has explained above. Still graph printing take O(n^2).

- KSS April 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But that would only work if victories are transitive. Apparently, there exists a method to do it even if they're not (see wiki on "tournament graphs").

- eugene.yarovoi April 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is topological sort. Also, in this problem is given that graph constructed out of player statistic, does not contain directed cycle (meaning a lost to b, b lost to c, c lost to a, for such case there is NO ordering possible)

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

After thinking on this question over an hour, and coming up with same conclusions as all the other commentors (topological sort, restricted to no cycle). I decided to try and solve with the dumbest, simplest solution first (my usual approach). It appeared that the question is much more simpler then we all thought.

We are all assuming that we need to implement a real-world sports ranking system; this is not the case. The way the question is phrased is actually not restrictive at all, you only need to satisfy two condition; player i must win against i-1, and player i must lose to i + 1. No where does it say that i + 2 must win against player i, nor does it say that we cannot print the same player more than once. I also realized that this solution may cycle, re-reading the condition "To print out a combination 1,2,3, .. , i-1, i, i+1,....n" means we only have to print up to n times. :P

- Emkay April 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I forgot to describe the actual algorithm. Step 1) arbitrarily pick a player, it is now player i. Step 2) print a player i + 1 who won against i. Step 3) Player i + 1 is now player i, repeat step 2) until we reached n prints. Complexity is n.

I'll give a more specific example. Consider this case of 5 players:
Player 1 won against every other player except player 5
Player 2 lost to player 1 but won against every other player
Player 5 won against player 1 but lost against every other player.

If I pick player 1, the output of my algorithm would be
1 5 2 1 5

- Emkay April 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, I think that's cheating. I do think the intent of the problem is probably to print every player exactly once. The correct answer is to have read the wiki article on "tournament graphs". There is a simple O(N^2) algo that will solve this problem. It uses an inductive approach: first construct a path for just 2 players, then incorporate a third player into that path (you can show there's always a way to do that), then incorporate a fourth...but all the players can be unique.

- eugene.yarovoi April 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I do not think it is cheating. A good software engineer designs simple and efficient algorithm that meets the specification; to presume more or to do less can lead to real-life disaster.

You are making assumptions that are not stated in the original problem. You assume the ordering of the series must imply transitive wins (if p1->p2, p2->p3, than p1->p3). However, since every player played against each other, it is possible for 1->2, 2->3, 3->1 to occur, creating a cycle. This causes a paradox in the "ranking" concept.

You can still use your algorithm to just print a list, and avoid cycling since the list is finite; but would this not be "cheating" as well?

- Emkay April 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What eugene is saying is correct, the intent of the original problem was to print out a combination of all players (with no players repeated). I was stuck at the point thinking whether a solution to this always exists or not. Also normal sorting algorithms won't work in this case, as i+1 has relation to only i, and no relation to i-1, so there is no global sorting parameter here.

- anuj78 April 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

"I do not think it is cheating. A good software engineer designs simple and efficient algorithm that meets the specification; to presume more or to do less can lead to real-life disaster."
_
I must disagree. I would say a good software engineer does not believe the specification when it doesn't make sense, goes back to the person providing the specification, and resolves any doubts about the spec with all the stakeholders. I would screw up so many things every day if I always listened to what a document told me I should do. People writing the specs make mistakes.
_
I would have asked the interviewer whether your approach is cheating or not, and designed accordingly.

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

Also, the algorithm for obtaining a path containing all vertices on a tournament graph does not assume a transitive ordering. It makes no assumptions that are not stated in the problem.

- eugene.yarovoi April 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@emkay
what you are saying automatically implies that i>i+2 otherwise graph will contain a cycle(i,i+1,i+2).
Also the algo that you described is O(n*n) as well as may get stuck e.g.
1
2 3
4
if found the path 1,2,4 there may not be a place to put 3

- mukul April 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

cannot understand the ques..plz help!!

- lilprogrammer July 11, 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