I have a number a let's say 390. I want to find smallest number which is greater than 390 and divisible by 180. Could't think of a good way feeling dumb

pawan chopra wrote:I have a number a let's say 390. I want to find smallest number which is greater than 390 and divisible by 180. Could't think of a good way feeling dumb

<putting on my math geek hat> In terms of available tricks, the number 180 is the product of 9 times 20. From this, we can say that whatever is divisible by 180, is also ...

* divisible by 20. Checking if something is divisible by 20 is simple -- just make sure the units digit is zero and the tens digit is even.

* divisible by 9. Checking if something is divisible by 9 is also simple -- just sum up all the digits, and the sum should be 9. And if the sum is more than one digit, repeat the test, by summing the digits of the sum.

So, I would start a loop at 400 (which is the smallest number greater than 390 that is divisible by 20), check if it is divisible by 9. If it is divisible by 9, you're done. If not, then add 20 and try again.

Doing this mentally... 400, 420, 440, 460, 480, 500, 520, 540... done. The answer is 540.

Well, if someone asked me that specific question. I will say the answer is

return 540;

If the want to know the answer for any given number, the answer is

return n/180 + n%180==0?0:1;

Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42965

73

posted

0

i think interviewer was looking for some tricky answer. He said loop is not good.

This is a terrible interview question. If he was serious about looking for something like Henry suggested then that guy has no clue how to identify good developers, and you're better off not working there.

It doesn't seem that bad. Using mod arithmetic is not really tricky, and, well, it is an O(1) solution rather than an O(n) solution.

Coding problems in interviews are often problematic - it's difficult to create a good problem that is really representative of the skills most important in a developer. But they can still be of some use for screening and for observing how a developer thinks. And to see how careful they are about details. I could see using this problem for a small part of an interview, letting the developer code a solution while I watched, to see how they do. I'd be most interested to see if they write tests to verify the corner cases, for example. If they gave a solution using a loop, I'd be OK with that, and if we still had time, I'd ask: what if the starting number was around a billion, rather than something like 390? That's a legitimate way to see if a mod-based solution occurs to the candidate. If they don't see it, well, it's not too big a deal. Accuracy is more important.

Here's an improved version, in the sense that it works correctly for a few more values than Henry's solution did. (Acknowledging that I shamelessly stole Henry's code as a starting point.)

This is still not entirely correct though. So, a challenge: in what cases is the above code still incorrect? How can it be fixed?

Mike Simmons wrote:...And to see how careful they are about details.

Exactly. Once the candidate has a solution that works for F(390, 180), do they consider F(360, 180), F(0, 180), F(-390, 180), F(390, -180), F(390, 0), etc.?