Amazon Interview Question for Software Engineer / Developers






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

use radix sort wid radix = 'n'...

- manni November 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

may be counting sort will help but the max element is N^2 there might be limitation on space

- Anna November 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//if der is no duplicate
1> bitset <n^2> arr;
2> for(int i=0;i<n;i++)
arr[a[i]]=1;//a is given array;
3>for(int i=0;i<n^2;i++)
if(arr[i]==1)cout<<i<<endl;

////////////if der is duplicates...use counting sort..

- Anonymous November 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the statement 3) runs till n^2... thus making the while logic O(n^2) which is not intended.

- Anonymous December 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//if der is no duplicate
1> bitset <n^2> arr;
2> for(int i=0;i<n;i++)
arr[a[i]]=1;//a is given array;
3>for(int i=0;i<n^2;i++)
if(arr[i]==1)cout<<i<<endl;

////////////if der is duplicates...use counting sort..

- Anonymous November 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

lets look at the complexity:
1: O(1) //assume
2: A loop that runs for each element i.e. n ==> O(n)
3. A loop that traverses through each element of arr whose size is n^2, no matter what the datatype of arr is the loop will run sizeof(arr) times i.e. n^2 => O(n^2)
total complexity: O(n) + O(n^2) ==> O(n^2)
please reply if my understanding is wrong or if there's something missing in the algo. thanks

- blueskin.neo November 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@above
dude... u have put loop over n^2... then how this solution is in O(n)

- Mogambo November 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

no dude, it`s 2n solution, not n^2. And 2n has O(n) complexity, constant (2) is ommited

- Anonymous November 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how can you say its 0(2n) if you are using a loop till n^2
You stupid.... You should not answer the questions ,if you dont know anything about complexity.

- Anonymous November 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Listen here, you idiot, it`s not my code above and you go now to the first post and see once again the task. LENGTH of array is N, not N^2. You don`t have to go till N^2 cause its range of elements, jackass. For decision we have to have additional array which size is N^2, but time complexity is O(n), go and learn complexity, boy.

- Homer J.S. November 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am confused here. At the risk of being called stupid/idiot let me say.
My understanding of O(n^2)
Something like as below. As n increases the number of loops needed increases by a factor of ^2.

for( ; i<n; ++i )
{
  for( j; j<n; ++j )
  {
  }
}

In the question, the range of values is till n^2, so as number of elements increases, by definition in the question, the range of value increases by a proportion of ^2.
After the first loop the values are still not sorted.
The second loop goes makes n^2 iterations, here the number of iterations increases by a factor of ^2. And the second loop is needed for the sort to complete.

Tell me if I am missing something.

- Kannan November 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you`re right, for one have to go through n^2 array, complexity will be O(n^2). Counting sort can`t dmake it in O(n) here.

If input array has uniform distribution of it`s data then a pocket sort may be used.

- Anonymous November 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If i am getting right, someone proposed an additional array of size n^2 so that you read elements one by one from the original array and put them in new array according to the index(assuming no duplicates). But while reading back the sorted array you have to again read n^2 spaces in new array. That still makes complexity of n^2.

The above strategy works when you have to sort n numbers in [0:m] where m = O(n)

- JoshMachine November 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, thats right. Its O(n^2)
If can just have a single loop till n^2 and say its O(n), I could have a bubble sort. :)

int j = 0;
for( int k = 0; k < n^2; ++k )
{
  int i = k%n;
  if( a[i] > a[j] )
  {
    swap(a[i], a[j]);
  }
  if( ++j == n )
  {
    j = 0;
  }
}

- Kannan November 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

fabinacii hashing using table size of N

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

Wondering: how is the fact the number is bounded by n^2 help/can be used to devise O(n) algorithm. Some number theory experts? Without that constraint it is just a normal sort problem of n unbounded numbers, which takes nlogn.

- Anonymous November 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Task is: try to sort in O(N), forget about O(nlogn) for a moment. There`s no need to be expert to know radix and pocket sortings, it`s algorithms` abc.

- Anonymous November 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the magic of bound n^2 is that you can divide each number to 2 parts, lower bits and higher bits. For example, for n ranges from 0...7, can be represented by 3 bits, n^2 ranges from 0...8^2-1=63 can be represented by 6 bits. Then you can first sort the lower 3 bits using counting sort, with O(n), then sort the higher 3 bits later with another O(n), which bring O(n) totally. Check Radix sort for more information if you would like.

- Anonymous November 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

a small correction, the complexity is O(n*logn), how?
well.. this is a specific case you described.
For n = 7, complexity is O(3n+3n) = O(n*2log8)-> O(n*2logn)
For n = 127, complexity is O(7n+7n) = O(n*2log128)-> O(n*2logn)
For n = 8191, complexity is O(13n+13n) = O(n*2log8192)-> O(n*2logn)

As you can infer, complexity is O(n*2logn) -> O(n*logn)

- Synonymouse November 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

My bad, if you are using counting sort for sorting the bits then it is O(n+k) where k = log2(n)
it is still and not O(n), rather than dividing into bits why not use counting sort directly? which should still be O(n+k)

- Synonymouse November 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the key point here is what is k? when you use counting sort directly, k=O(n^2). However, when you divide into 2 parts by using counting sort twice, in each counting sort, k=O(n). That is why you get O(n) finally.

- Anonymous November 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

so you sort the lower and then higher, fine . how do you combine these together?

- Synonymouse November 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

to be simple, we take 2 digits integers for example, such as array A=[25,33,15,47,56,18]. First we sort the number based on the lower digits and get B=[33,25,15,56,47,18], the lower digits' order is 3,5,5,6,7,8. Next we sort the new array based on the high digits, to get C=[
15,18,25,33,47,56], this time the higher digit's order is 1,1,2,3,4,5. Since the counting sort is stable, we get the same order as in B for those that has the same number in high digits, like 15,18.

I hope it's clear, but if it's still not, check the following link,
oopweb.com/Algorithms/Documents/AnimatedAlgorithms/VolumeFrames.html
look up item 5 Radix Sort by Woi Ang to view Animation.

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

clarification: are duplicates allowed in the original array?

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

One way to do this would be to create a temp array of size n, each element of which is a binary tree (why? because you'll be inserting elements into this tree in a sorted form, so you want to make sorted inserts as cheap as possible). iterate over the source array. any element from 0..n-1 goes to temp[0], from n to 2*n-1 to temp[1], etc (basically a[i]/n). Insert the elements into temp[j] in their correct location relative to the rest of the elements in temp[j]. Once you've done that, you have an array of sorted data structures that is itself sorted; in other words, every element of temp is either null or a sorted data structure such that every element in temp[i] is greater than every element in temp[i-1] for all i. Iterate and print; you're done.

- nj November 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In the worst case, all of the numbers are in one binary tree, then you have O(log(n)).

- Passerby December 23, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i agree with nj's method.... if worst case, all elements end up in a single location, then inorder traversal of that bst will take O(n)..

overall retrieving, building tree n placing in original array (alongwith traversal) wont exceed O(n)..

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

I am assuming you are talking about a BST and not just a binary tree. Whatever the case, in the worst case, just to form the BST itself will take O(nlogn).

- anonymous January 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It is very easy and naive to do in O(n^2).
By quick sort- O(nlogn).
for O(n), here is my solution, please correct me if I am wrong:
Since the maximum element is N^2, we will stort the value based on the following algorithm:
i) map=number/N is the mapping value i.e it always fall it between 0-N, but with different map.
ii) index=Number%N which places the above value at position a[index](kinda Hash table), where index<N. so the size of this is array is N.
keep a counter array(dynamic like linst list, whenever a value is inserted at the index i, then add a node with value map), which actual stores value of every variable which is inserted.
user defined data struture would be like this:

struct ar{
int value; // lies between 0-N
struct *node
{
int map;
struct *node next;
}
}

ar[N];
each ar[i] contain all the values which are i,n*m +i .

- Aditya November 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution can be bucket sort. Each element divide it by 'N' and put it into one of the buckets 0..N. Then sort these buckets individually and finally merge buckets.

- coolpk November 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if all the elements fall in same bucket?

- Anonymous December 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What if we just sort by SQRT?

- Random December 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I believe Young tableau should be used to hold n^2 elements , then we can treat this one as heap . Complexity would be linear.

- Kunal January 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry, Young tableau wont work in this case

- kunal January 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can use a hash map with Integer as key and value is the list of Integers
The size of the hash map is N.

we scan the original array of size N and with values ranging from 1-n^2
divide ever value by n and then place it in the appropriate bucket in the hash map.

Also if already elements are present in the list then we place it at an appropriate position.

eg: suppose if N=5 we have an array

3 11 2 24 22

3/5=0 map<0,3>
11/5=2 map<2,11>
2/5=0 map<0,2>// now the list is maintained as 2->3 as we place it at the correct spot during the insert itself
24/5=4 map<4,24>
22/5=4 map<4,22>

so the resulting map looks like

key	value
0	2->3
1	null
2	11
3	null
4	22->24
5	null

thus on performing a linear scan on the hash map we get the sorted list
complexity is O(n+n+1) = O(n)

- ketz January 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi..
I guess this wont work in all cases.
x/y is a double right?
So the algo works upto the precision of double
x/y and q/y can both give same number up to some precision.
They will only be different for infinite precision.

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

But still that occurs rarely. So we can use radix sort when that occurs,i.e if two double precision digits are same. This gives average complexity of O(n).
But still worst case pie(nlogn)

- Shashi February 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Folks please read radix sort.

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

I dont think Radix sort will work because it needs all the numbers to have same number of digits...
I dont think we can use say 1 as 0001.

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

I dont think Radix sort will work because it needs all the numbers to have same number of digits...
I dont think we can use say 1 as 0001.

- Anonymous January 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

scan the array and find the numbers less than equal to N. USE a hash table of size N and sort these numbers.

Again mark the numbers greater than N. Again USE the hash table of size N and hash value = arry[i]-N. and sort these numbers.

merge the two sorted list.

O(N)

- amm February 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Again.. I havent read all solns.. but as i understand it .. question says .. the range of numbers is n^2 ... while the length of array is n.... sorting it using any normal sorting method (Quick, Heap etc)should be fine...

- RaZ March 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I Know I am going to give nonsense solution which is having Large space complexity but Range is given n^2 so we can take an array of n^2 and update array subscript as per value let me explain with an example

int a[n^2];

I/p: 12,12,4,16,11,1,1,1,4,6,2

a[1]=3
a[2]=1
a[4]=2
a[6]=1
a[12]=2
a[16]=1

So Sorted Nos. are We 1,2,4,6,12,16 frequency is also stored .Leave Subscript having zero value ,It is very very basic very much space consuming but method is covered in O(n)

Plz Don hit me :)

- User April 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Guys, Convert numbers to base 2 from base 10..and then do a radix sort - O(n)

- Hari April 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I meant convert to Base N

- Hari April 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

There is no way you can get it in O(N). You have to access the number atleast once which will make it O(N^2)

- Anonymous November 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This isn't correct

- Anonymous November 20, 2010 | 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