Microsoft Interview Question for Software Engineer / Developers


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




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

Count the number of set bit at even and odd position .....if their difference is divisible by 3 then number is divisible by 3

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

but how can this be proved?

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

If you are getting infinite number of bits then how can you count the number of set bits. This answer does not fulfill the criteria.

~Harjit

- Harjit Singh March 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Harit..
We can keep the difference with mod %3

for eg: if no. of set bit at odd position is 6 and no of set bet at odd position is zero we can reset the no of set bit at odd position to zero again since at this point number is divisivle by 3

so we don;t need to keep the infnite value we can reset the value once the no is divisible by 3

@Cartman

for prove

we can prove in similar way as we proved for divisibility of 11

abc =100a+10b+c = 99a +11b + (a-b+c)


(a-b+c) should be divisible by 11 inorder to make abc divisible by 11.

in the similar way we can prove for 3 also

- NaiveCoder March 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote
{{{#include<stdio.h> #include<math.h> int i = 0; int sum = 0; void divisible_by_three(int d) { sum = sum + (pow(2,i)*d); if((0 == d && 0 == i) || (1 == d && 0 == i)) printf("\nThe binary stream is not divisible\n"); else { if(0 == (sum % 3)) printf("\nThe binary stream is divisible\n"); else printf("\nThe binary stream is not divisible\n"); } i++; } void main() { int d; int ch; do { printf("\nEnter the binary number (0 or 1)\n"); scanf("%d", &d); divisible_by_three(d); }while(1); } - Prathamesh March 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I dont think this is correct. The input is an infinite stream of bits how do you handle sum overflow? You can't simply convert the number to 10-base.

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

True . Cant convert the binary to decimal . NO data type can be used to store variable of infinite size .

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

A number is divisible by 3 if the sum of its digits can be divided by 3.
so have a buffer to add all the digits so at any point of time you can tell whether the number is divisible by 3 or not

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

@Harish Venkat
The input is in binary format. Your technique would have worked if input was in base 10.

- Learner March 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

count per each input char

count = (count + 1) % 3

return count==0;

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

We can keep tracking the remainder. Here is my answer to the question:

basicalgos.blogspot.com/2012/03/28-test-whether-binary-stream-can-be.html

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

void divisible_by3(bitstream bits) //hypothetical infinite stream of bits
{
  int d = 0, bit;
  for (;;){
   bits>>bit;
   d <<= 1;
   d += bit;
   d %= 3;
   if (d == 0) {
      cout<<"divisible";
   } else {
     cout<<"not divisible";
   }
  }
}

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

note: this code assumes number is given from most significant to least significant bit

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

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char c;
int even=0,odd=0
boolean flag;
while(cin>>c)
{
if(flag)
{
if(c=='1')
even++;
}
else
{
if(c=='1')
{
odd++;
}
}
flag=!flag;
}
if((even-odd)%3==0)
cout<<"Divisible by 3"<<endl;
else
cout<<"Not Divisible by 3"<<endl;
getch();
return(0);
}

- VIneet Setia March 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1365 => 10101010101 is not divisible by but it will divisible by your algorithm.

- Rajendra Prasad June 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The logic is as follow:
1. Every number can be expressed as n=3a+b where b<3.
2. Thus if we assume the current number we have is n and when a new binary character i comes, the new number is n'=n*2+i=6a+2b+i.
3. We can approve that if 2b+i is divisible by 3 then so is n'.
To sum up, we only need to record the remainder of current number (which is b) and when a new binary character i comes we check if 2b+i is divisible by 3. It will never overflow or have any other problems.

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

This was the solution i was looking for .
{{
void isbitstreamdivisible ()
{
int n = 0 ;int k =0;
while(T)
{

n= getbit();
if(k = (2k+n) %3 == 0)
printf ("Divisible by 3");
else
printf("not divisible by 3");

}

}



}}

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

It's just calculating the binary stream and converting it into decimal. it will not work for infinite bit stream.

- Rajendra Prasad June 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

make use of automata .... moore / mealey machine ,,, the interviewer probably wants to know just that and not how you will store an infinite stream of data or find its length

- aryan shah March 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Aryan Shah, I also took the way of using Deterministic Finite State Automata

/*assuming infinite input fed from command line*/
#include<stdio.h>
int main()
{
int input,state=0;
//state indicates the remainder when the stream is divided by 3
while(scanf("%d",&input)!=EOF)
{//assuming all time valid input is given
switch(state)
{
case 0: if(input==1) //initially remainder is 0 if 1 is pushed then clearly remainder should be 1 else it should remain 0
state=1;
break;
case 2:if(input==0) //initially remainder is 2 if 0 is pushed then clearly remainder should be 1 else it should remain 2
state=1;
break;
case 1:if(input==1) initially remainder is 1.if 1 is pushed then remainder is 0 and if 0 is pushed then remainder is 2.
state=0;
else
if(input==0)
state=2;
}
}
//if remainder=state=0 finally then the number
if(!state)
printf("Divisible");
else
printf("Not Divisible by 3");
}

- Mohit Ahuja March 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Apply the same logic as for numbers of base10 no difference... :)

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

int main()
{
int bit_stream=0, remainder=0;
while (TRUE)
{
bit_stream = get_bit();
remainder = remainder * 2 + bit_stream;
if( remainder % 3 == 0 )
{
printf("Divisible by 3");
}
else
{
if ( remainder > 3 )
remainder = remainder % 3;
}
}/*while loop close*/
}/*main close*/

Test Cases:
Pass Test Cases: 1111, 1100, 10101
Fail Test Cases: 101, 111

- Rajendra Prasad June 10, 2012 | 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