Symantec Interview Question: Division method without using... | Glassdoor

Interview Question

Associate Software Engineer Interview Herndon, VA

Division method without using divide operator


Interview Answer

5 Answers


int divide(int a,int b){
//should return a/b
if (b == 0){ throw new DivisionByZeroException(xxxx);} // or do whatever...
if (b == 1){return a;}
int i = 1;
int temp = b;
while (temp

Jason Peng on Nov 12, 2012

i++ should be before temp=b*i, sorry for error

Jason Peng on Nov 12, 2012

The other way is to use bitwise operation
(>> or >), you are doing division of 2.
For example,
01000 represents 8
00100 represents 4
00010 represents 2
00001 represents 1
Hopefully you can understand this....
However, you need to take care of when the denominator is odd- which is very difficult to do.
I would recommend my first solution listed above. It is more understandable or less error-prone.

Jason Peng on Nov 12, 2012

Use >> to shift both until divisor is 1, the final dividend value is the answer

Anonymous on Nov 6, 2015

Someone from Symantec gave me information about what to expect in the job interview at Rooftop Slushie.

Anonymous on Jul 18, 2019

Add Answers or Comments

To comment on this, Sign In or Sign Up.