Interview Question






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

convert int to char* and add all digits.. continue this recursively until the sum reaches 1 digit. if the sum is 3/6/9, then it is divisible by 3

- kiran April 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

itoa is needed in this case to get char* from int

- Kiran May 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ak
what we we have to find whether the number is divisible by 3 or we want to implement this number /3.

for checking divisibility solution given by "kiran" is good.

Otherwise. (n>>1) -1

- Anonymous April 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Did you try that solution on anything other than n=3?

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

n=6 rather.

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

we can definitely use repeated subtraction to do the division.
But in question it is given that itoa() is availbale.

What is the use of this information here ?

- manish sindhi April 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

(n>>1) -1 logic doesn't work all the time check out with 21....
Repeated subtraction with a counter looks be cool if performance is not an issue...
Still wondering for a solution using itoa()...

- Surya April 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming it is a positive integer and you are looking for integer (i.e. 5/3 = 1 and not 1.666666) then we can use binary search.

You double your estimate(x) for n/3 till you hit one for which x+x+x>n. Now you can binary search the range based on the previous value and current value of x.

- Anonymous April 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you explain this more clearly??

- Erik April 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

n/3 = pow(2, log2(n) - log2(3))

- Anonymous April 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

geeksforgeeks.org/?p=511

- Guarav May 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The simple way of doing that will be , convert the given number into string using itoa, then add get the sum of the given number, check whether sum is 3 6 or 9 , if yes then its divisible :)

- Any August 20, 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