Interview Question


Country: United States




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

bool patternCheck(int a)
{
int b=a>>1;
/*if a=000000101010 then b=0=00000010101,,,,rightshift of a by 1*/
//now xor of a and b
int n=(a^b)+1;//n is in 2^n format if alternate bits are there
if(n & (n-1)) == 0) return true;
return false;
}

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

Hi,
Can you please explain why we are doing n+1 after n=a^b and what n&(n-1) does ? Because, the same condition ( if(n&(n-1) ==0) is used to check if the number is a power of 2.
Thanks for your patience.

- ps April 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 4 vote

This comment has been deleted.

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

cool!!!!!!!!!!!

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

consider the number... a=01110 => b=00111
and a^b=01001 => return true.... which is wrong result as 0's and 1's are not alternate....

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

You can just write a for loop and right shift by one bit each time.
And use flag to check for alternate 1's and 0's.

- sefuns January 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I wasn't sure of the question until I start reading answers. But what if numbers such as BB are consider "alternating"?

- EthOmus June 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

And more, how about "110110110110"; in fact, we are not even sure how long is the smallest alternating unit, which \could be as long as a byte or two byte; at least not until after testing halve of all bits

- EthOmus June 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

hey, i am confused about the first example.
since the first 6 bits are not alternating pattern, why would it return true?

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

can anybody pls tell me wat actually test cases mean... i.e. wat interviewer wants us to write when he asks for the test cases... it wud be gr8 if u can give a small eg... thanks ...

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

//num represents the equivalent decimal
//logic is simple implementation using finite automata
// i am assuming tat strings like 0001010,00000101 are considered as
//alternate.
//the code is simple but i'd like to get a more compact code



int alternate(int num)
{
int a=0;

if(num&1)
{
a=4;
num>>=1;

while(num)
{
if((a==4)&&(num&1))
return 0;

else if((a==4)&&(!(num&1)))
a=5;

else if((a==5)&&(num&1))
{
a=4;

if(num==1)
a=5;
}
num>>=1;
}
}

else
{
a=1;
num>>=1;

while(num)
{
if((a==1)&&(!(num&1)))
return 0;

else if((a==1)&&(num&1))
a=2;

else if((a==2)&&(!(num&1)))
a=1;

num>>=1;
}
}


if((a==5)||(a==2))
return 1;
return 0;
}

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

bool alternate(int x)
{

int y = (x*3);
y>>=1;
y+=1;
if((y & (y-1))==0)
return true;
else return false;
}

am i missing something.. this sounded really simple.

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

The bits are coming in as a stream

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

the statement y+=1; is redundant.
It should work without it as well.
nice code snippet.

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

int findAlternate(int num)
{
int n=1;
while(n <= num)
n<<=1;
n-=1; n = n^num; n<<=1;
return( n==num?1:0);

}

- Aalok June 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think shortest & effective way-

isAlternate(int num)
{
  return (num & 0x5555)==0 || (num & 0xaaaa)==0);
}

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

bool isAlternate(int x){
If((x & 0xAAAAAAAA)==x)
return true;
}
Assuming int is of 4bytes.

- Anonymous May 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool isAlternate(int x){
If((x & 0xAAAAAAAA)==x)
return true;
}
Assuming int is of 4bytes.

- Mayukh May 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public boolean isAlternate(int num)
{
  return (num ^ 0x55555555)==0 || (num ^ 0xaaaaaaaa)==0);
}

- Abhishek Kothari May 29, 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