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.

- Anonymous September 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Specifically, a splay tree.

- Anonymous September 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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?

- Anonymous September 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

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

- Anonymous September 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I s'pose m log n is doable FWIW.

- Anonymous September 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Anonymous September 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

43256 is larger still.

- Anonymous September 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

321456.

- Anonymous September 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous September 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Anonymous September 05, 2011 | Flag
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!

- anonymous September 05, 2011 | Flag Reply
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>

- Anonymous September 07, 2011 | 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