Microsoft Interview Question






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

If somebody comes up with the answer he can patent it....

- DashDash August 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

patent psycho patent psycho :P :P

- kathirvel September 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

patent psycho patent psycho :P :P

- kathirvel September 14, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not possible

- code bug August 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

does the value of each items has a scope?
then we can use counting sort.
otherwise,i have no idea

- anonymous August 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I can think of nlogn as many of you would. Traverse through the list and create a btree But cannot think of O(n) to apply in linked list

- Golu August 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I can think of one solution.

1. Run through the list and find the MAX. TC = O(n).

2. Create a vector with size MAX and initialize to 0 or something else. SC = O(MAX)

3. run through the original array and using value of each element in the array as the index for your vector , set the vectors value to 1. TC = O(n)

4.Now run through the Vector and store the elements back in the array who's values are set to 1. TC= O(MAX)


So total Time complexity is = 2O(n) + O(MAX) ~= O(n)
So total Space complexity is = O(MAX)

- CodeBoyS August 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Worst case memory hog but Works!

- Gautam August 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@CodeBoyS what if the input is something like ->
-10000000 10000000 ........

- Frodo Baggins August 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Create a bigger array with 2 * Abs(MAX) elements and consider the minimum on index 0.
The problem is when the array is bigger than the memory.

- S August 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Complexity is defined on the size of the input and not on the input value, so for the above sol, the complexity denoted as 2O(n)+O(MAX) is actually 2*O(n)+O(2^(log MAX))
(as a number n is represented by log n bits).
Now if the second term dominates, then the algo turns out to be exponential, not O(n).

- Triton August 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Triton: you are right... but if you tell the interviewer that you are going to use 4GB of memory (which is O(1) - constant), I think s/he will be very unhappy with the answer... :)

The solution with the array is O(n) when Abs(a[i]) = O(n).

- S August 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort in O(n)? Give me a break.

- Anonymous August 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

le le bhai break..chai pani pi ke aana :P :P

- Anonymous September 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

bhai bahut break ho gaya wapas aa jaa ab

- unknows November 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Kit kat break to banta hai.

- Anuj September 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

aja mahi aja maho...aa soniye
kaha gayab ho gya??

- pranav November 17, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// k = maximum value of any element of the given array -> O (n)

for i ← 1 to k do // -> O (k)
c[i] ← 0
for j ← 1 to n do // -> O (n)
c[A[j]] ← c[A[j]] + 1
//c[i] now contains the number of elements equal to i
for i ← 2 to k do // -> O (k)
c[i] ← c[i] + c[i-1]
// c[i] now contains the number of elements ≤ i

x = 0
for j ← 1 to n do
if (x<j)
{ while (x!=j)
{ c[A[j]] ← c[A[j]] - 1
b[A[j]] <- A[j]
x ++
}
}



// solution is the b[j] array

- Sayak August 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

linked list don't have random access iterator. so you can't do CS on linked list. can possible when you copy linked list element to array A and then again copy B to linked list

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

sayak we are taling about linked lsit sorting and then its the count sort which requires a lot of memory and that too you can use when you know the range of numbers only...if A[j]=10000 what will be your B array's size ?? did you know what is the maximum element in the array ??

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

yes we can implement it in O(n)
1.find max element in linked list- O(n)
2.create an array of size max
3.traverse linked list and put element of liked list in to correspinding index in array suppose read element of linked list is 5 then it has to inserted at arr[5]
4.finally array is sorted and we can copy array element back to linked list
time complexity-O(n)
space complexity-O(n)

- ramu kumar September 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

think before u write dude..
1. how will u take care of the duplicates
2. suppose there is no element called 5 ?
3. n how is it O(n) ?
4. so max element is 30 and there is no number from 1-20 ?
..what will do u of the 20 elements in the array created..

not discouraging u but think before u write :)

- @ramu .. September 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ramu: think it twice before posting!!

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

ramu is a comedian? :p

- chenzhaohonggd September 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

U r direction is good, do not consider people discouraging you...you can do better...Thank of using HashTable,,or something like TreeMap in Java...You can achieve in O(N), i am sure, you just need to think little harder. Good luck !!
@Rest: if you have balls, give the answer, or else get the fuck out of here, u morons !! \m/

- @Ramu September 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use radix sort which will sort the list with O(kn) time and O(kn) space where k is the number of digits (or chars) in the longest key.

- erm October 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

why not we use counting sort instead to sort the array which is o(n)?

- Arup July 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

copy the linked list to an array..sort the array in O(n)..copy it back to the linked list..whats wrong with this one?

- apoorv July 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Great..but how do you sort an array in O(n)?! :P

- NoOne August 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

why not create a entirely new linkedlist. fetch an element from the current list and parse the new list to find the position of this pointer. simultaneously free the node which has been arranged in the new linked list.

- sparta October 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes, We can do it as we don't have to worry about the space complexity. We remove the head of the linked-list and insert this node into a Min-Heap. After inserting all the elements of linked-list into priority -queue we repetitively remove the head of the Min-Heap and insert back into linked -list.

- Manish August 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Are you kidding?! :)
Well, we can do it if quantum computing comes practical!!

- sathishp123 November 18, 2013 | 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