• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic maths (May be simple)

 
pawan chopra
Ranch Hand
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you need a loop that starts a count at 0, and then increments the count by 180 until it is larger than X.
 
pawan chopra
Ranch Hand
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is what I thought but could not convince
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you don't think it could be made to work? Why not?
 
pawan chopra
Ranch Hand
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will work for sure but i think interviewer was looking for some tricky answer. He said loop is not good.
 
pawan chopra
Ranch Hand
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is better

(Input +divider) - ( input mod divider)
 
Henry Wong
author
Marshal
Pie
Posts: 21490
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21490
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...

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
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ryan McGuire
Ranch Hand
Posts: 1078
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic