Adobe Interview Question for Applications Developers


Country: India
Interview Type: In-Person




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

This will be optimized using the following logic. The operation num%8 will have a value greater than 0 only if the least significant 3 bits of a number are set. This is because of the fact that least significant 3 bits represent 2^0=1, 2^1=2 and 2^2=4. Any other bit that is set will be a multiple of 8 (8,16,32...) and will be useless for num%8 operation as the result will be always 0.

So we can just add the least significant 3 bits to the sum.

sum = 0;
while (!EOF)
{
sum += num&0x7;
}
if (sum & 0x07)
// not divisible by 8
else
// divisible by 8.

So basically we have removed the % operator and substituted it with the much faster bitwise operator &. Also since we are using only the lower order 3 bits for sum calculations the possibility of overflow is also less (until it's told that we are dealing with thousands of numbers in each file)

- tarutrehan June 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

smart logic

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

for files with millions of numbers: this can be modified as: after adding the least significant 3 bits of the number to the sum, since we have to check only least significant 3 bits of the sum in the end, we can do and operation between sum and binary(111).

In that way, at any time, we have store at max number 7 in the variable sum.

Correct me if i am wrong!!!

- Udit June 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If there is a possibility of overflow then we must do this step. It's correct to avoid overflow.

- tarutrehan June 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sum = 0;
while (!EOF)
{
sum += num&0x7;
if ( (sum & 7) == 0 ) 
  sum = 0;
}
if (sum & 0x07)
// not divisible by 8
else
// divisible by 8.

In this way we can also avoid the overflow.

- Psycho September 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

int isdiv8(FILE*fp)
{

int total 0;

while(fscanf(fp, "%d", &num))
{
total += num % 8;
total %= 8;
}

if (!total)
return 1;
else
return 0;

}

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

Find sum of the numbers in each file keeping in mind that at any time the sum < 8. If sum >= 8 then sum = sum %8. Now the final remainder if 0 will mean the sum of numbers is divisible by 8.

- Srikant Aggarwal May 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In this problem, I think the type of mathematical operations done play a role. In your solution, sum%8 would be done N number of times. There could be another solution where we add all numbers and then the final sum%1000, will give last three digits, which we can divide by 8 and check.

- nehadhardce May 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You need to check for overflow as well, reset it at 65528.....

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

We need to just check that in binary representation of the number last 3 digits should be 0. This can be checked by the condition (no && 7 == 0).

- Anonymous June 07, 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