# Basic maths (May be simple)

posted 2 years ago

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

SCJP - itspawan.com

Ulf Dittmer

Rancher

Posts: 42967

73

Ulf Dittmer

Rancher

Posts: 42967

73

posted 2 years ago

It will work for sure but i think interviewer was looking for some tricky answer. He said loop is not good.

Pawan Chopra

SCJP - itspawan.com

posted 2 years ago

I think this is better

(Input +divider) - ( input mod divider)

(Input +divider) - ( input mod divider)

Pawan Chopra

SCJP - itspawan.com

posted 2 years ago

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.

Henry

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

**<taking off my math geek hat>**

Henry

posted 2 years ago

Well, if you are going to do it with mod math, then in pseudo code ...

~~if (390 ^ 180 is zero) return 390~~

~~else~~ return ((390 / 180) +1) * 180;

[EDIT: Just noticed that the result has to be greater than 390, so modified the pseudo code above.]

Henry

pawan chopra wrote:I think this is better

(Input +divider) - ( input mod divider)

Well, if you are going to do it with mod math, then in pseudo code ...

[EDIT: Just noticed that the result has to be greater than 390, so modified the pseudo code above.]

Henry

Ulf Dittmer

Rancher

Posts: 42967

73

posted 2 years ago

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.

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.

Mike Simmons

Ranch Hand

Posts: 3076

14

posted 2 years ago

It doesn't seem

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?

*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?