Country: United States

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

If we have something like a1 > a2, a2 > a3, a3 > a1, we know that there is no ascneding order. So we can create a graph based on the comparisons. Create nodes for each element. and then if element x > element y, direct the graph from node y to x. Run some sort of graph algorithm like DFS with cycle detection. If we detect a cycle, we know that we can not order in ascending order.

Comment hidden because of low score. Click to expand.
0

what if there is no relationship established between any two of them. say a1>a2,a2>a3,a4>a3 . There would be no cycle but you cannot sort them as you dont know the relation between a4 and a1 or a2. You need to make sure that the graph is complete graph first and then check for a cycle. If not reject it straight away

Comment hidden because of low score. Click to expand.
0

a

Comment hidden because of low score. Click to expand.
0

a

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

I think this can be done by topology sort , if a1 > a2 then draw an edge from a2 to a1, using this way to setup a graph, then do a topology sort for the problem

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

Sort in ascending order of the first elements using quick sort. Then try to find the location of the second element in the sorted list using binary search on the right hand side of its greater number.

You may also sort the second part and then do one time merge. In any of these methods the running time will be O(n log n), because you have n/2 items that you don't know their order.

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.

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