## Adobe Interview Question for Applications Developers

• 0

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)

Comment hidden because of low score. Click to expand.
0

smart logic

Comment hidden because of low score. Click to expand.
0

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!!!

Comment hidden because of low score. Click to expand.
0

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

Comment hidden because of low score. Click to expand.
0

``````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.

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;

}

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.

Comment hidden because of low score. Click to expand.
0

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.

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.....

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).

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.

### 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.