Microsoft Interview Question for Software Engineer in Tests


Country: India
Interview Type: Phone Interview




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

I think all those solutions are nice. I, however, will just convert the two numbers to String in java and then swap the digits as the person has asked. Convert it back to number..

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

You are not expected to convert it to String.

- Kishore Jinka September 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

NOTE: ((~0)<<i)^((~0)<<j) ---> will give a mask of type 0001111000 where first 1 from left starts at position j and last 1 is at position i.

{{
unsigned int set(unsigned int n, int i, int j, unsigned int m, int k, int l) {
unsigned mask = ((~0)<<l)^((~0)<<(k));
mask = mask & m; // get the bits between k and l from m
mask = mask >> l; // Removes the trailing 0's
mask = mask << j; // Pushes the bits of interest to left by j

unsigned mask1 = ((~0)<<i)^((~0)<<(j));
mask1 = ~mask1; / Invert the mask
mask1 = mask1 & n; // get all the bits except bits between i and j from n

return (mask1|mask);
}
}}

- CareerCup September 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It can be solved with bitmask .. Firstly we take a copy of bits in n & m and replace them .. here is a simple code

void solve ( int &a , int &b ,int i , int j , int k , int l )
{
	vector<int> aa, bb ;
	for ( int co=i ; co<=j ; co++ )
		aa.push_back( a&(1<<co) ) ;
	for ( int co=k ; co<=l ; co++ )
		bb.push_back( b&(1<<co) ) ;
	for ( int ii=0 ; ii<aa.size() ; ii++ )
	{
		if ( aa[ii] )
			b |= (1<<ii+k) ;
		else
			b &= ~(1<<(ii+k));
	}
	for ( int ii=0 ; ii<bb.size() ; ii++ )
	{
		if ( bb[ii] )
			a |= (1<<ii+i) ;
		else
			a &= ~(1<<(ii+i));
	}
	return ;
}

- unknown September 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

n_mask = (2^(l-k-1)-k)<<i; // one can optimize powering with 2

bit_copy = (n & n_mask) << (l-j); // Assuming (l-k) == (j-i)

m_mask = (2^(j-i-1)-1)<<i;
m_mask = ~m_mask; // flip bits;

m = (m&m_mask)|bit_copy;

- Anonymous September 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Guys please right a simple algo , instead of code or write both...that would be much easier to understand

- Brijesh September 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void solve(int &a,int &b,int i,int j,int k,int l){
int pa=i,pb;
int tempABit,tempBBit;
for(pa=i,pb=k;pa<=j;pa++,pb++){
tempABit = (1<<pa)&a;
tempBBit = (1<<pb)&b;
tempBBit = tempBBit>>pb;
tempABit = tempABit>>pa;

a = a & (~(1<<pa));
a = a | (tempBBit<<pa);

b = b &(~(1<<pb));
b = b | (tempABit<<pb);

}
}

- wangzhengnb September 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can't we simply store these integers in array and try this code :
n=j-i+1;
while(n)
{
i[m]=k[n];
i++;
k++;
}

- Nishant Pandey September 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Another way without bitwise operations:

#include <iostream>
#include <string.h>
#include <sstream>
#include <stdio.h>
#include <math.h>

using namespace std;

int replaceBits (int m, int n, int i, int j, int k, int l) {
        int bitsInN = n/(int)(pow(2, k)) % (int)pow(2, l+1);
        int bitsInM = m/(int)(pow(2, i)) % (int)pow(2, j+1);
        int res = m - (bitsInM-bitsInN) * pow(2,i);
        return res;
}
int main() {
        printf("%d\n", replaceBits(3072, 170, 3, 5, 5, 7));
        return 0;
}

- euv921 September 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<math.h>

using namespace std;

int main(void) {
int m;
int n;

cin >> m ;
cin >> n ;

int i,j,k,l;
cin >> i;
cin >> j;
cin >> k;
cin >> l;
int m1, m2, n1, n2, lm1, lm2, ln, ans;

lm1 = (floor(log2(m)) + 1) - i + 1;
lm2 = (floor(log2(m)) + 1) - j;
cout << "lm1 " << lm1 << endl;
m1 = (-1 << lm1) & m;
m2 = (m & (int)(pow(2, lm2) - 1));

ln = (floor(log2(n)) + 1) - l ;
n1 = ((n & ((int)(pow(2, l - k + 1) - 1) << ln)) >> ln) << lm2;

cout << m1 << endl;
cout << m2 << endl;
cout << n1 << endl;

ans = m1 + m2 + n1;

cout << ans << endl;;

return 0;
}

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

//Simply, I would like to separately get (cut of)  the bits from position k to 
//position L from number n
//This can be done be shifting number n to the left (31-L) times
//Then Shift to the right ((31-L) + k )times. This will make the number n only 
//contains the bits from position k to position L. 
//These bits will be at the beginning (right side) of the 
//number n. Shifting n to the left i  times will position the bits of n in the 
//required position of m. Finally do OR operation between m and n. 

void Main()
 {
            uint m = 0x00000C00;
            uint n = 0x000000AA;
            Console.Write("i: ");
            int i = int.Parse(Console.ReadLine());
            Console.Write("j: ");
            int j = int.Parse(Console.ReadLine());
            Console.Write("k: ");
            int k = int.Parse(Console.ReadLine());
            Console.Write("l: ");
            int L = int.Parse(Console.ReadLine());
            n <<= (31 - L);
            n>>= ((31-L)+k);
            n <<= i;
            m |= n;
            Console.WriteLine("{0:X}", m);
}

- Ayad September 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

thanku nice solution...

- laasya1991 October 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its a question directly from Gayle's book.

- sp October 21, 2011 | 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