Amazon Interview Question for SDE1s


Team: Marketplace Team
Country: United States
Interview Type: Phone Interview




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

It is just 11 bits. It makes 2048 numbers. You can build an array of 2048 elements and count how many time every number appeared.

- Bob September 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why we need to count the number of times the number has appeared. Its no where mentioned in the question.

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

what about radix sort as we have size of each integer..

- jimmy October 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

For this problem, we cannot do better than linear time.

Counting sort is sufficient enough!

- ninhnnsoc September 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes it would be enough given that 11 bit only represents positive numbers

- Mahmud September 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

int[] input = {10,20,60,58,45,10,200,2001,485,95,85,545,458,487,545,120,121,2020,1,2,10,20};
        int[] arr = new int[2024];
        for(int i=0;i<arr.Length;i++){
            arr[i] =0;            
        }
        
        for(int i =0;i<input.Length;i++){
            arr[input[i]] +=1;  
        }
        
        for(int i=0;i<arr.Length;i++){
            if(arr[i]!=0){
                Console.WriteLine(i+ "---> " + arr[i]);
            }
        }

- poweruser September 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As its says 11 bits - we should probably use Radix Sort

- Mahmud September 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

bingo.

- ravio September 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is just 11 bits. It makes 2048 numbers. You can build an array of 2048 elements and count how many times every number appeared.

- Bob September 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using constant memory and in linear time (θ(n)) in Java.

public static void main(String[] args) {
        int[] input = {4, 1, 4, 5, 512, 6, 1, 3, 754, 5, 523, 67, 51, 8, 9, 345, 599, 2032, 2011, 300, 12};
        int[] counts = new int[2048];
        for (int i = 0; i < 2048; i++) {
            counts[i] = 0;
        }

        for (int number : input) {
            counts[number]++;
        }

        int ptr = 0;
        for (int count = 0; count < 2048; count++) {
            for (int i = 0; i < counts[count]; i++) {
                input[ptr] = count;
                ptr++;
            }
        }
        System.out.println(Arrays.toString(input));
    }

- DmitryS September 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This looks O(n^2) to me

- shr1234 September 21, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

@shr1234 then you would be wrong.

for (int count = 0; count < 2048; count++) {

This is constant time, not linear.

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

Probably the

for (int count = 0; count < 2048; count++) {
            for (int i = 0; i < counts[count]; i++) {

that he's talking about.

- Anonymous November 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void sort(int[] arr) {
	if (arr == null || arr.length < 2) {
		return;
	}

	int[] res = new int[1 << 11];
	for (int i : arr) {
		++res[i];
	}
	for (int i = 0, pos = 0; i < res.length; pos += res[i++]) {
		Arrays.fill(arr, pos, pos + res[i], i);
	}
}

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

How about converting each 11 bits to number. Put it in array and then use Arrays.sort().

- Anonymous September 28, 2014 | 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