Microsoft Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

main()
{
int a=2347869;
int x=a,new_number=0;
int arr[10]={0};
int i,temp;
while(x)
{
temp=x%10;
arr[temp]=arr[temp]+1;
x=x/10;
}
for(i=9;i>=0;i--)
{
while(arr[i])
{
new_number=new_number*10+i;
arr[i]--;
}
}
printf("%d ",new_number);
}

this algo takes constant space.....using array of size 10

- Pankaj February 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is smart...

- xiuxiu February 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

smart, and fast.
I was still writing the code when you've already submitted it :)

- amustea February 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This does not take care of negative numbers

- Vinay March 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int max_num(int num) {
        bool is_neg = num < 0;
        if(is_neg) {
            num = num * -1;
        }
        int count[10];
        for(int i = 0; i < 10; ++i) {
            count[i] = 0;
        }
        while(num != 0) {
            ++count[num % 10];
            num /= 10;
        }
        if(!is_neg) {
            for(int i = 9; i >= 0; --i) {
                while(count[i] != 0) {
                    num = num * 10 + i;
                    count[i]--;
                }
            }
        } else {
            for(int i = 0; i < 10; ++i) {
                while(count[i] != 0) {
                    num = num * 10 + i;
                    count[i]--;
                }
            }
            num = num * -1;
        }
        return num;
}

- smffap March 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What are these lines of code doing??


for(i=9;i>=0;i--)
{
while(arr[i])
{
new_number=new_number*10+i;
arr[i]--;
}
}

- Anonymous March 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Smart solution and well done ! This is what interviewers most probably are looking for during technical interviews. As for accounting for the negative numbers, I think it's just a matter of checking the sign before starting extracting the individual digits and keeping a flag for that.

- ashot madatyan March 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

To answer Anonymous:

for(i=9;i>=0;i--)
{
while(arr[i])
{
new_number=new_number*10+i;
arr[i]--;
}
}

The former lines of code created a histogram representation of the number. The while loop uses the elements of the histogram, one at a time from 9->0. The arr[i]-- indicates that one element of the histogram has been used up.

- mike March 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

one solution could be:

1. Make an array of size 10 countDigitMap[10] = {0};
2. Start taking out least significant digit of number.
3. and keep wcountDigitMap[digit]++;
4. loop from end form ( 9, 8 ... 0 ) and print digits in map with count > 0.

guys if u have any better than this one please comment below.

- loosy.jhony February 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How is this any different from the first solution posted?

- Rayden February 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I posted this algorithm before "@Pankaj" solution above. The only difference is that I just wrote the algo. rather than code.

- loosy.jhony February 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

one big mistake that i saw in arun and pankaj.
u have taken the number in int.. int has limitation..

below code takes jst the input char by char and put outs in the order.

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
	char c;
	int a[10]={0};
	while((c=getchar())!='\n')
	{
		a[c-'0']++;
	}
	for (int i=9;i>=0; i--)
	{
		while(a[i])
		{cout<<i;
			a[i]--;
		}
	}
	
	return 0;
}

- dhandeepm March 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the limitation u are talking about with the "int" is about the new number crossing the range of the integer am i right???

- pankaj March 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

int digit(int n , int l)
{
for(int i = 0; i<l i++)
{
n = n/10;
}
return n%10;
}

int biggestNumber(int number)
{
n1=number;
int n1, n2, l=1,
while(n1>9)
{
n1=n1/10;
l++;
}
n2=number;
for(i=0; i<l; i++)
{
d1= digit(n2 , i);
for(j=0; j<l; j++)
{
d2= digit(n2, j);
if(d1>d2)
{
n2= n2 - (d1-d2)*pow(10 , i) + (d1-d2)*pow(10 , j);
}}
}
return n2;
}

- arun February 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

actually some changes will increase the performance as well but i just tried to explain my approach with constant memory ...

- arun21.nit February 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int digit(int n , int l)
{
for(int i = 0; i<l i++)
{
n = n/10;
}
return n%10;
}

int biggestNumber(int number)
{
n1=number;
int n1, n2, l=1,
while(n1>9)
{
n1=n1/10;
l++;
}
n2=number;
for(i=0; i<l; i++)
{
d1= digit(n2 , i);
for(j=0; j<l; j++)
{
d2= digit(n2, j);
if(d1>d2)
{
n2= n2 - (d1-d2)*pow(10 , i) + (d1-d2)*pow(10 , j);
}}
}
return n2;
}

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

int digit(int n , int l)
{
for(int i = 0; i<l i++)
{
n = n/10;
}
return n%10;
}

int biggestNumber(int number)
{
n1=number;
int n1, n2, l=1,
while(n1>9)
{
n1=n1/10;
l++;
}
n2=number;
for(i=0; i<l; i++)
{
d1= digit(n2 , i);
for(j=0; j<l; j++)
{
d2= digit(n2, j);
if(d1>d2)
{
n2= n2 - (d1-d2)*pow(10 , i) + (d1-d2)*pow(10 , j);
}}
}
return n2;
}

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

The solution provided is correct. The idea is somewhere along the lines of counting sort rather than insertion sort. Insertion - in any circumstance is quadratic and not linear.

- Ketan February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

its just a case of counting sort. O(n) . limit of each digit 0-9

- V February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can this be done using quick sort of digits in descending order? it will be O(nlogn)

- mtsingh February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

do not need count array.

int digit(int num, int bit) //bit numbered from 0
{
return num/(pow(10.0,bit))-(int)(num/(pow(10.0,(bit+1))))*10;
}

int biggestnum(int num)
{
int result=digit(num,0);
int countend=num;
int i=0;
while((int)(countend/10)!=0)
{
i++;
int j=0;
int d=digit(num,i);
while(j<i&&d>digit(result,j))
{
j++;
}
result=result%(int)(pow(10.0,j))+(int)(result/(int)pow(10.0,j))*pow(10.0,j+1)+d*(int)pow(10.0,j);
countend/=10;

}
return result;
}

- luckycat March 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use bubble sort because it is simple and it use a constant space + 1 cell for the swap.
Order from high to low.

#define SIZE 3
int tab[SIZE];
int swapped=1;
while(swapped)
{
  swapped=0;
  for(i=0; i<SIZE-1 ; i++)
  {
    if(tab[i]<tab[i+1])
    {
      temp = tab[i];
      tab[i] = tab[i+1];
      tab[i+1] = temp;
      swapped=1;
    }
  }
}
for(i=0; i<SIZE ; i++)
{
  printf("%d", tab[i]);
}

- CheckThisResume.com March 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

what about insersion sort?

- hitman February 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Use minimum , constant space.

- aak February 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Interesting but I cant figure out how to implement in this case. Could you provide some code?
I was thinking about inserting digit at the proper bit index, then iterate through each bit to generate the number. But it won't work when the number has duplicate digits.

- Anonymous February 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Interesting but I cant figure out how to implement in this case. Could you provide some code?
I was thinking about inserting digit at the proper bit index, then iterate through each bit to generate the number. But it won't work when the number has duplicate digits.

- Anonymous February 29, 2012 | 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