Microsoft Interview Question
Software Engineer in TestsIf there are only two type elements(0/1) then go through all the elements, maintain two counters which would count # of 0's and 1's. Finally replace the whole array with x # of 0's and then y # of 1's.
This algo would take O(2n)=O(n) steps
Prince sol is nice using bucket , but same can be done using less no of insertion and deletion just start two counter one from start and one from end,keep on incrementing start till 1 encountered by start counter and 0 encountered by end counter then swap them . stop doing this when start is grater then end.
I answered Bucket sort or quick sort are the best for this prob. Any suggestions?? Is there a better algo for this problem?
- Sadineni.Venkat January 11, 2009