Interview Question

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

Find the largest number in positions 0..n, move it to position 0, recurse on the rest of the array with the remaining moves. When implemented with a max-decorated binary tree, this algorithm is O(m log m), where m is the size of the array.

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

Specifically, a splay tree.

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

Any balanced tree will do I suppose, not necessarily splay trees.

And it is O(n log n) not O(m log m): Do you even care about elements n+1 to m?

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

Yes, because we could have n = 1 and an input like

m m-1 m-2 ... 2 0 1

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

I s'pose m log n is doable FWIW.

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

btw, 0...n is not right I think. You have to ignore the initial decreasing sequence..

For instance n=2 and you have

32456

You will return 32456, but 35246 is larger.

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

43256 is larger still.

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

321456.

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

I spend 0 to keep 3 where it is and then recurse on 21456 -> 42156.

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

Sorry I had a brain fart. Yes, this looks right to me now.

O(m log n) is doable using a max-heap of n elements.

Basically the left most number can only come from [0,...n] and if that can be changed, it is always better to change it. Then we try to determine the second left most and so on.

One possibly problem might be with repeated numbers, but in that case I guess we can choose the max which was the left most in order to save some movement cost.

Nice solution.

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

@eepiyush: Why spam CC with OLD posts only.
Go to stackoverflow, and post there, your moron!

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

<pre lang="" line="1" title="CodeMonkey851" class="run-this">class MakeItLargest {

public static void main(String[] args) {
println(makeItLargest(new int[] { 1, 2, 3, 4, 5 }, 3, 0));
println(makeItLargest(new int[] { 1, 2, 3, 4, 5 }, 4, 0));
println(makeItLargest(new int[] { 1, 2, 3, 4, 5 }, 5, 0));
println(makeItLargest(new int[] { 1, 2, 3, 4, 5 }, 6, 0));
println(makeItLargest(new int[] { 1, 2, 3, 4, 5 }, 7, 0));

}

public static int[] makeItLargest(int[] a, int n, int start) {

if (n <= 0) {
return a;
}

int max = Integer.MIN_VALUE;
int maxIndex = -1;
for (int i = start, length = start + n; i <= length && i < a.length; i++) {
if (max < a[i]) {
maxIndex = i;
}
}
for (int i = maxIndex; i > start; i--) {
int tmp = a[i];
a[i] = a[i - 1];
a[i - 1] = tmp;
}

makeItLargest(a, n - (maxIndex - start), start + 1);

return a;
}</pre><pre title="CodeMonkey851" input="yes">
</pre>

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.