Amazon Interview Question for Software Engineer / Developers






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

can u explain last line elaborately..

- nav October 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Question , Given an integer K, right code to print array permutation at Kth position.

- Anonymous October 12, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Question Still not clear. Can u give the expected output?

- neer October 13, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Keeping the ith position constant, print all the other permutations?

- Anonymous October 21, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think, it means the following:
If we generate all the permutations of a given array and store these permutations in another array say PermuatedArray. Now we sort this PermuatedArray. The Answer to the question will be, PermuatedArray[k] in the sorted version of PermuatedArray.

Hope this makes it clear enough.

- Anonymous November 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static public int[] FindKthPermutation(int[] input, int k)
{
if (k < 0 || factorial(input.Length) <= k)
throw new Exception("K invalid range");

Array.Sort(input);

bool[] used = new bool[input.Length];
for (int i = 0; i < used.Length; i++)
used[i] = false;

int[] output = new int[input.Length];


int outIdx=0;
for (int i = input.Length-1; i >0; i--)
{
int fact = factorial(i);
int pos = (int)(k / (double)fact);

int j = 0;
while (used[j])
j++;

int skipped=0;
// make an array of unused
while (skipped<pos)
{
if (!used[j])
skipped++;
j++;
}
output[outIdx++] = input[j];
used[j] = true;

Console.WriteLine("i:{0}, fact:{1}, k:{2}, pos:{3}, j:{4}", i, fact, k, pos,j);

k = k % fact;
}

// find the last unused digit
int x= 0;
while (x < input.Length)
{
if (!used[x])
break;
x++;
}

output[outIdx++] = input[x];
return output;
}

- smp December 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The basic algorithm of the above code is this:
sort the input array in ascending order
at each digit position i, find the next digit location using the possible range for a given k.

So, we don't need sorting here. Just pick the right index in every iteration.

- smp December 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

get k th value.. store the value at kth place and store the number k..
on the remaining items.. do the regular permutation.
when even u are printing the output.. insert kth value at kth position and output it..

- clrs March 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@smp for k=8 it is giving 2213 as output.

- XYz May 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

assume the set as 1,2,3,4 and rank is 8

- XYz May 05, 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