Microsoft Interview Question for Software Engineer / Developers


Team: Bing
Country: India
Interview Type: In-Person




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

circular_shift(int num, int k, int dir) {
    if(dir)    //assume dir = 1 for left shift
        return (num << k) | (num >> (sizeof(int) - k));
    else return (num >> k) | (num << (sizeof(int) - k));
}

- cooldaa January 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

cooldaa, what will happen on k > sizeof(int)?

- ric January 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

correct solution,
with minor correction, it should have been
8*sizeof(int)

- abhishekatuw January 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks for the correction @abhishekatuw :)

- cooldaa January 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ric: As abhishekatuw corrected it is actually 8*sizeof(int). But if are talking about total number of bits, you may add the code for bounds checking, because if k > sizeof(int)*8, all bits will be tossed out of the number.

- cooldaa January 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if k is greater than 32, then after each 32 shift operations you will be left with the same number.
do something like
if(k > 32)
k %= 32;
if(k == 0) return num;
else
//do what cooldaa did

- hello world January 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I considered integer as 4 bytes as in JAVA. but in general
k > 8*sizeof(int)

- Dev January 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if k > 8*sizeof(int), expression (8*sizeof(int) - k) will become negative. The compiler will then flag runtime error as the right operand of shift operators has to be a positive integral quantity.

- cooldaa January 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That is why you will do k = k % 32. That works!!

- Dev January 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

in here,

(num << k) | (num >> (sizeof(int) - k));

when u do num << k, then aren't yo u already loosing the k bits ? since, it will pad them with 0's. So when it reaches (num >> (sizeof(int) - k), num is changed already, with k bits lost ! So,, wont be a cyclic shift.

Am I missing something ?

- P January 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, Actually shift operators do not modify the original number. The result of shift operation is returned in a temporary variable. So the value of num is preserved. This is a great advantage of shift operators.

- cooldaa January 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int circular_shift(int num, int k, char dir) {
    int n = 1;
    if (dir)         //assume dir = 1 for left shift
        n = ~n;   // for left shift, n will hold 1 in the highest bit position
    while(k--) {
        if(dir) {    
            if(num&n)  //if highest bit holds 1;
                num = num<<1 + 1;  //then add 1 to the left shift
            else num = num<<1;    //otherwise, just simply shift
        } else {
            if(num&n)      //if lowest bit holds 1
                num = (num>>1)  | ~n;   //add 1 to highest bit
            else num = num>>1;        //otherwise just make simple right shift
        }
    }
}

- cooldaa January 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <cstdlib>
#include <intrin.h>

#pragma intrinsic(_rotr8, _rotr16)

int main()
{

	int inputInteger = 376;
	unsigned int offset = 3; // bits to roate
	
	std::cout << " Original Number: " << inputInteger << "\n";

	inputInteger = _rotr16(inputInteger, offset);
	//inputInteger = _rotl16(inputInteger, offset);		// for left rotate

	std::cout << " Rotated Number: " << inputInteger << "\n";
	getchar();
	return 0;

}

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

/**
@shpos - how many positions to shift (left or right)
@dir - left (-1) or right(1) shift
*/
int cyclic_shift(int num, int shpos, int dir)
{
int retv;

if ((0 == shpos) || (sizeof(int) * CHAR_BIT == shpos))
return num;

/* Prevent losing any bit of the original value */
shpos = shpos % (sizeof(int) * CHAR_BIT);

if (dir > 0) // the right cyclic shift
retv = (num >> shpos) | ( num << (sizeof(int) * CHAR_BIT - shpos));
else // the left cyclic shift
retv = (num >> ( sizeof(int) * CHAR_BIT - shpos )) | (num << shpos);

return retv;
}

- ashot madatyan February 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int circular_shift(int num, int k, int dir) {
int numBits = Integer.toBinaryString(num).length();
if(dir == 1) //assume dir = 1 for left shift
{
k = k %numBits;
return (num << k);
}
else{
k = k %numBits;
return (num >> k);
}
}

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

def maximizeProfit(input):
    
    maxDiff = float("-inf") 
    currentDiff = float("-inf")
    bottom = input[0]
    max = 0
    for index in range(1,len(input)):
        currentDiff += input[index] - input[index-1]
        
        if (currentDiff > maxDiff):
            maxDiff = currentDiff
            max = input[index]
            
        if (bottom > input[index]):
            print(currentDiff)
            bottom = input[index]
            currentDiff = 0
        
    print("buy at :"+str(max -  maxDiff)+ " Sell at: "+str(max))

- Anonymous April 11, 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