Amazon Interview Question
Software Engineer / Developers//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..
//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..
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
no dude, it`s 2n solution, not n^2. And 2n has O(n) complexity, constant (2) is ommited
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.
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.
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.
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.
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)
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.
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.
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.
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)
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)
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.
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.
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.
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)..
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 .
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.
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)
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.
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.
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 :)
use radix sort wid radix = 'n'...
- manni November 20, 2010