Microsoft Interview Question for Software Engineer / Developers


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




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

I think the key point here is that the stream is of *infinite* length.

Any solution that keeps track of all bits or their summation or in any other form would cause overflow problems.

The solution needs some number theory to prevent keeping info of all incoming bits. The right way to do this is to only keep the REMAINDER.

Notice the truth that X%3 and X have the same remainder, when divided by 3. So having X%3 is enough to decide whether X is divisible by 3.

When new bit comes, multiply the remainder by 2 (if bit is 0) or *2+1 (if bit is 1). Then repeat the procedures of keeping remainders.

- liangzhongliang March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I think this is right answer.

while(1)
{
if (input) k = k*2 +1;
else k = k*2;

k = k%3;
if (k == 0 ) //current can be divisible by 3.
}

- Ethan April 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

We can apply the finite state automata on this problem.

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

Similar solution: added by hitman in another thread..

bool divisibleBy3(bool next_bit)
{
  static int remainder = 0;
  remainder = ((remainder * 2) + (next_bit ? 1 : 0)) % 3;
  return (remainder == 0);
}

- AA August 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please explain the solution further, I still don't get it

- crystal.rishi2 October 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice approach.
But this only works if we are getting bits from MSB to LSB, but what if the bit stream is from LSB to MSB?

- IntwPrep December 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
6
of 6 vote

keep track of the number of set bits (1's) in the odd positions, and the number of set bits in the even positions.

if the absolute difference of both values is divisible by 3, then the number is divisible by 3.

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

any mathematical proof for this argument?

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

here is the proof.
Our goal is to show that every 2^k number could be represented as 3*p +/- 1 (plus or minus) form, and also if 2^k = 3p + 1 then 2^(k+1) = 3*q - 1 and vice versa.

first part is very easy. every integer can be represented in one of the following 3 forms: 3*p - 1 (or the same is 3*q + 2), 3*p or 3*p + 1. Note that 2^k <> 3*p as 2^k does not contain 3 multiplier, so 2^k = 3*p - 1 or 2^k = 3*p + 1.

now assume 2^k = 3*p - 1. Then 2^(k+1) = 2 * (3*p - 1) = 6*p - 2 = 3 * (2*p) - 2 = 3 * q + 1. And the same we can do for 2^k = 3*p+1.

- Artur Sokhikyan March 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Didn't really understand the proof, but this is very similar to divisibility by 11 for a base 10 number as 3 is 11 in base 2.

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

It would be a fair objection to say, however, that storing the number of occurrences of anything requires O(logN) space, since the number counting the occurrences will have that many digits. It's possible to solve this in O(1) space by simply keeping a state machine with 6 states.


Have a function f: current remainder (0, 1, or 2) x incoming digit (0 or 1) -> new remainder (0, 1, or 2)


It isn't hard to determine what this function should be simply by modular arithmetic

0 remainder x 0 incoming digit -> 0
1 remainder x 0 incoming digit -> 2
...

How these numbers are derived is described in the other well-rated response.

- eugene.yarovoi March 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

At start keep teh count of number of zero till you encounter first '1' bit. At this time calculate the decimal number.
Now keep updating it using the left shift concept.
Left shift with 0 doubles the number.
Left shift with 1 2*number+1
Once you have the number it is easy to check that it is divisible by three or nor.

- King@Work March 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

@kingwork: "At this time calculate the decimal number.".. this is the challenge.. how can you calaculate decimal representation of an infinitely long stream.. and which datattype will you use to store it ??

- ashish March 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

in an integer, if the sum of digits is divisible by 3 then whole integer is divisible by 3.
for ex- 9, 24, 36, 99, 111, 213, 2124 etc.
If by some mean we can get the sum of the digits on the go then we can solve this problem.

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

let we already n bits have gone and currently we have n+ 1 th bit.
for n bit we know the flag then calculate the flag for n+1 th bit as:
—-----------------------------------
n+1 th bit. last flag. new flag
0. 0. 0
0. 1. 2
0. 2. 1
1. 0. 1
1. 1. 0
1. 2. 2

proof: let last number with n bits: (3k+flag)
so next number will be 2(3k+last flag)+(n+1th) bit.
whenever flag is zero it will be divisible by 3.

- ptr.earnmoney March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Instead of calculating the number everytime, since the stream is infinite, this can be done as follows.

#include<stdio.h>

main()
{
	char ch='\0';
	unsigned short int remainder=0, n=0;
	while(1)
	{
		scanf("%c",&ch);
		n++;
		if(ch=='1')
		{
			if((n-1)%2==0)
				remainder = remainder + 1;
			else
				remainder = remainder + 2;
		}
		else if(ch=='0')
		{
			remainder = remainder + 0;
		}
		else
			break;
		if(n==2)
			n=0;
		if(remainder%3==0)
		{
			printf("Yes\n");
			remainder = 0;
		}
		else
			printf("No\n");
	}

}

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

I think this problem can be solved using the fact that if sum of digits of a number is divisible by 3 than that no is also divisible by 3. now if that sum is divisible by 3 than sum of its digits is also divisible by 3. so if we keep adding digits of this sum until we get a single digit(<9), than this digit must be divisible by 3.

now here problem is that, we have to play with bits. but than can also be dealt in same way as we can deal it with decimal nos. here is my solution(i tested this with different inputs and it is working fine)

int main()
{
int sum=0;
while(1)
{
c = getNextBit(); // returns next bit as char from stream
if(c!='0' && c!='1')
break;
if(c=='0')
sum = sum*2;
if(c=='1')
sum = (sum*2)+1;
if(sum>9)
sum = addDigits(sum);
if(sum%3==0)
printf("%c:sum %d is divisible by 3\n",c,sum);
else
printf("%c:sum %d is not divisible by 3\n",c,sum);
}
return 0;
}

int addDigits(int s)
{
int sum = 0;
while(s)
{
sum += (s%10);
s /= 10;
}

if(sum>9)
return addDigits(sum);
else
return sum;
}

as here i am keeping sum as single digit, so even with infinite length stream, overflow problem will not be there.

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

private static void IsDivisibleBy3()
        {
            int counter = 0;
            bool evenOddFlag = true;
            while (true)
            {
                var nextBit = Console.ReadKey().KeyChar;
                if(nextBit == 'x')
                    break;
                if (nextBit == '1')
                {
                    counter += evenOddFlag ? 1 : -1;
                }
                evenOddFlag = !evenOddFlag;
                if(nextBit == '0' || nextBit == '1')
                    Console.WriteLine(counter % 3 == 0 ? "\tDivisible" : "\tNot divisible");
                else
                    Console.WriteLine("\tIncorrect bit value");
            }
        }

- Artur Sokhikyan March 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

agree to the code of ashish..simple..but just one drawback..what happens when you run out of int and you have not found a single multiple of 3 so that num is not 0 anywhere...pasting my code...I have not dealt with endianness in this code i.e. right shifting will always mean a number multiplied by 2.

int main(void)
{
    int bit;
    short sum = 0;
    int divisor;
    scanf("%d",&bit);
    while((bit == 0)||(bit == 1)) {
                short t = 0;
                t = sum << 1;
               if(!sum)
                       sum = bit;
               else if(t < 0) {
                    divisor = sum << 1;
                    sum = -1;
                    divisor = divisor % sum;
                    sum = divisor;
                    sum = (sum << 1) + bit;
                    }
               else {
                   sum = (sum << 1) + bit;
                }
               if(sum %3 == 0) {
                      printf("\ndivisoble by 3\n");
                      sum = 0;
                      }
               scanf("%d",&bit);
    }
    return 0;
}

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

introcs.cs.princeton.edu/java/73dfa

- Anon April 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;
 
bool isDivisible(string stream, int divisor) {
	int rem = 0;
	string::iterator itr = stream.begin();
 
	while(itr != stream.end()) {
		rem = ( 2*rem + (int)(*itr - '0') ) % divisor;
		itr++;
	}
	cout<<rem<<endl;
	return rem == 0;
}
 
int main() {
	string stream = "1001010110010101001010010100101001010100100101001010010101011101";
 
	cout<<"isDivisible by 3 : "<<(isDivisible(stream, 3)? "true" : "false")<<endl;
	return 0;
}

- xcode January 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;
 
bool isDivisible(string stream, int divisor) {
	int rem = 0;
	string::iterator itr = stream.begin();
 
	while(itr != stream.end()) {
		rem = ( 2*rem + (int)(*itr - '0') ) % divisor;
		itr++;
	}
	cout<<rem<<endl;
	return rem == 0;
}
 
int main() {
	string stream = "1001010110010101001010010100101001010100100101001010010101011101";
 
	cout<<"isDivisible by 3 : "<<(isDivisible(stream, 3)? "true" : "false")<<endl;
	return 0;
}

- xcode January 19, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

here is my solutionn: the interviewer said it is fine.

int bit;
int num=0;
while(true){
scanf("%d",&bit);

if(bit==0){
num=num*2;
}

if(bit==1){
num=num*2+1;
}

if(bit!=0 && bit !=1){
printf("%s","invalid input.");
}

if(num%3==0){
num=0;
printf("%s","is divisible by 3");
}
else{
num=num%3;
printf("%s","not divisible by 3");
}

}

- ashish March 25, 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