Microsoft Interview Question


Country: United States




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

I think its basically to Swap Nibbles..In the firstcase find the mask That is Number of Bytes / Nibbles needs to be Swapped...Count the number of Bits /Bytes given as input make.The mask will be 00FF for 2 bytes for (4 hexa Characters) And 000FFF for 3bytes for (6 Hexa characters.)

Then Just perform the operations
First case - k1 = 243b & 0 0 FF
k2 = 243B >>(No of bytes / 2 )*8in the input
Then k = k1 | k2 is the answer
Note - Our Mask will also be Number of Bytes / 2 ---- Numbers of F's
In first case input is of 2 bytes so Mask is of 1 byte = 00FF
In 2nd case input is of 3 bytes so mask is of 1.5 = 000FFF

- Lucy October 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I got what you want to say but do you think the order will be restored which apparently is the main problem here.

I came out with similar approach:

1) 3b24 AND FF= 24 = K1
2)3b24 >> 2^3= 3b = K2
3) 24 << 2^3 =2400 = (K1 << 8) =K3
4) Output= K3+K1= 243b

- coder_aspirant October 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

This code will work
int i=0x3b24; //if it is 16 bit compiler
int res=0X0;
res=(i<<8)|(i>>8);

- pmahesh.417 February 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As it is not string literal but an integer literal
i tried to split the elements in the array and put arr[]={'0','x','3','b','2','4'} and swap the elements arr[2] with arr[4] and arr[3] with arr[5].

The code was to be written in C. I could have used perl/python split and join but I am unaware of any such method for integers in C.
Also the output has to be integer so putting the elements separately in arr[ ] doesnt seem to me a good idea. Anybody having any different approach?

- coder_aspirant October 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

some more examples for question,
can the size be fixed?

- smile October 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Use snprintf:

int n = snprintf(NULL, 0, "%0x", value);
char hexStr[n + 1];
snprintf(hexStr, sizeof hexStr, "%0x", value);

- Godel October 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Below code snippet should work for swapping the nibbles

int val = 0x3b24;
int rev = 0;

rev = (val & 0xFF) << 8 | (val & 0xFF00) >> 8;

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

int convert(int num)
{
	if(num<0x10)
		return num;
	else if (num<0x100)
		return (num&0xf<<4)||(num&0xf0>>4);
	else if (num<0x1000)
		return (num&0xf<<8)||(num&0xf00>>8)||(num&0xf0);
	else if (num<0x10000)
		return ((num&0xff)<<8)||(num&0xff00>>8);

}

- Charles August 02, 2014 | 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