Electronic Arts Interview Question for Software Engineer / Developers






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

while(binaryNumber > 0){
  if( (binaryNumber % 2) == 1 ){
    count++;
  binaryNumber >> 1;
}

- Anonymous September 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

int mask = 0x01;
	int count = 0;
	while (mask)
	{
		if ((num & mask) == mask)
			count++;
		mask <<= 1;
	}

Basically, we have a number:
xxxxxxxx
And we try to check whether each bit is set or not. So we start off with the extreme right (least significant) bit, 0x01, which will be 00000001 (in case of an eight bit number). Remember: regardless of the number of bits of the number, the LSB will always be 1 for 0x01.
Now we bitwise AND it.
xx x xx xxx
&00000001
If the last bit is 1 then we should get this same 0x01 number back.
The logic is the same for the rest of the bits. To get them, we simply left shift the mask every time by 1.
And once the mask is 10000000, and then it is shifted again, it becomes 00000000.
Thus mask is not non-zero any longer, and quits the while loop.

- Chris September 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

count = 0;
while (num = num & (num - 1))
   count++;

- Anonymous August 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

This is a good solution but at the very last step, suppose we have the value 00000010 and then we clear the bit. Then the value 0 will be assigned to num, which is false. Hence it doesn't count the last 1. Thus it is better to separate the statements like so:

count = 0;
while (num)
{
  num &= num-1;  // Note that there has to be atleast one 1 if the number is greater than 0. If it isn't, it won't even enter the while loop
  count++;
}

- Chris September 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//When a binary number is stored i an array//
for(i=0;i<a.len();i++)
if(a[i]/1==1)
c++;

- Sumanth Vakacharla November 22, 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