Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

10 bit means there's only 2^10 = 1024 possible values. Use counting sort. Create an array of 1024 integers and in the i-th index keep the number of occurrences of the i-th possibility. IN the end, just traverse your occurrence count array printing each element the number of times that it occurs.

- eugene.yarovoi June 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Amazing answer, just amazing. I could not come up with that, I lost my job probably on it.

- kamoliddin June 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is a classic and very well-known approach. This is an important sorting algorithm to know. Still, I wouldn't count you out just because you didn't know this one thing with this one interviewer. Were you able to solve the problem using a different technique?

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

stunning!

- sriram June 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Just a small correction - we will need 1023 elements in the array instead of 1024. So, given that and the counting sort algo, you can easily solve it in O(N) time.

- ashot madatyan June 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I knew the sorting algorithm u mentioned, I did not focus on 10 bit, and went to wrong direction.

- kamoliddin June 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Small optimization on counting sort: Use array of bits (e.g. bitset<> in C++). When parsing through the list, just set the corresponding bits. This will save the memory too.

- Victor June 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, counting sort is powerful if the range is known. In just one parse, you get the result.

- Victor June 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Victor: bitset won't work. You actually need the count of each element.

@ashot madatyan: why 1023? You want to have a special case for the 1024th one where we don't count it and fill up any space left at the end with it? It would use less than 0.1% less space but add implementation complexity.

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

I agree, in this particular case, count is needed and hence small optimization in the space should be that each array element for counting sort is max 2 bytes to accommodate max count of 10000.

- Victor June 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you want to understand this concept of counting sort, you can watch a very good video lecture at
youtu.be/w9njRpeuVew

- coolvisualization June 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

There is a problem if repetitions are allowed. In that case, array of linkedlist needs to be created...

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

Repetition can be handled if the array is made of positive integers of two bytes each. And then apply counting sort.

- Victor June 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can also use the radix sort

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

Complexity of Radix sort is higher than Counting sort. If you are aware of any optimization to make Radix sort faster, please share.

- Victor June 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Radix sort will work, and for 10 bits, it's still quite fast. But why use something like 10 passes when you can use 1?

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

int[] input = new int[10000];
            Random random = new Random();
            for (int i=0;i<1000;i++){
            input[i]=random.Next(0, 1024);
            
            }
            Sorting.CountingSort(input);

public static void CountingSort(int[] arrayA)
        {
            Console.WriteLine("Original Array");
            for (int i = 0; i < arrayA.Length; i++)
            {
                
                Console.Write(arrayA[i] + " , ");
            }
            Console.WriteLine("");
            int k = 1024;
            int[] arrayB = new int[arrayA.Length];
            int[] arrayC = new int[k];
            for (int i = 0; i < arrayC.Length; i++)
            {

                arrayC[i] = 0;
            }
            for (int j = 0; j < arrayA.Length; j++)
            {
                arrayC[arrayA[j]] = arrayC[arrayA[j]] + 1;
            }

            //Place the number of elements less than each value at i into array C.	
            for (int i = 1; i < k; i++)
                arrayC[i] = arrayC[i] + arrayC[i - 1];

            //Place each element of arrayA into its correct sorted position in the
            //output array B.
            for (int j = arrayA.Length - 1; j >= 0; j--)
            {
                arrayB[arrayC[arrayA[j]] - 1] = arrayA[j];
                arrayC[arrayA[j]] = arrayC[arrayA[j]] - 1;
            }

            //Overwrite the original arrayA with the output arrayB.
            Console.WriteLine("Sorted Array");
            for (int i = 0; i < arrayA.Length; i++)
            {
                arrayA[i] = arrayB[i];
                Console.Write(arrayA[i] + " , ");
            }

            Console.WriteLine("");

        }

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

why this code is required:


//Place the number of elements less than each value at i into array C.
for (int i = 1; i < k; i++)
arrayC[i] = arrayC[i] + arrayC[i - 1];

- some_name June 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

en.wikipedia.org/wiki/Counting_sort

- ajaypathak June 28, 2012 | Flag


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