Microsoft Interview Question
Software Engineer / DevelopersTeam: bing
Country: India
Interview Type: In-Person
int bit;
int state = 0; // 0 : divisible, 1: remainder is 1, 2: remainder is 2
while ( (bit = getNextBit()) != NULL)
{
switch (state)
{
case 0:
if (bit == 0) state = 0; // remain as divisible
else state = 1; // change state to "remainder is 1"
break;
case 1:
if (bit == 0) state = 2;
else state = 0;
break;
case 2:
if (bit == 0) state = 1;
else state = 2;
break;
} // end of switch
} end of while.
Assuming that the bits from the stream are treated as added at the least-significant (right) end:
- YG March 27, 2012