Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Honestly sounds like a bad interviewer. repeated subtraction is a good first step and I would try to push more if that was the first implementation. But If you could derive a base 10 remainder you know conceptually what problem the mod operator is trying to solve.

a % b = a - (b * a/b) /assuming a sane language with integer division, else cast a/b to int/

Figuring the above operation (or getting close) is when you should more or less pass, and That's a good point to show the interviewee what the operation is. That should be the point of an interview problem, to show the thought process, not that you know fancy tricks.

But alas, I was shown an XOR swap in an interview last week and spent 3 minutes deriving it on paper instead of 3 seconds saying "oh yeah, a => b and b => a" to a trick that I haven't seen since college some decade ago. The current market loves tricksters, I suppose.

And yes, the actual real world use of modulo is surprisingly sparse, despite easily imagining situations where you could use it.



I am highly reluctant to use type casting as a mathematical function!! I was burnt by early languages struggling with this problem... Even modern languages still have issues with this! Try running this in Python3.11

``` float(9007199254740993) == int(9007199254740992) ```




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: