Interview Question - Divide without Division
I have been interviewing programming candidates recently and was thinking, what’s a good ‘programming’ question? Some criteria:
- A question which a candidate can solve in an interview setting.
- A type of question which every programmer should know how to solve, whether they are new to programming, or a veteran programmer.
- One that doesn’t require a lot of explanation.
- A problem which can be made harder or easier, depending on the candidate.
- Programming language agnostic.
- Works as a coding question and as a white board question.
- Multiple ways to solve the problem.
One such question
For me, one question that I have found which fit the above criteria is:
Write a divide function without using the division operator
Basically, it is to reimplement mathematical operator divide: ‘/’. I found this in the first chapter of my favorite algorithms textbook.
This question works as a whiteboard question or even as a coding question. There are many solutions to this question, depending on the scope. There isn’t a lot of explanation required as well.
The most important thing I’m looking for: is the candidate communicating with me while they are solving the problem? What kinds of questions are they asking? What are they thinking? Why?
Feedback from friends
I’ve been given some applause and grief for this question from my programming friends. Some of these friends have worked in top tier software companies. Some have said it’s a great question and this question is on the list of a top tier software company.
Either case, I will work through the problem here to show it’s not that hard and what I’m expecting.
My implementation in Ruby
In this case, I will use Ruby’s divide method (
/) to provide verification of
my implementation of divide. I don’t want to think too much about the test
result. I rather have Ruby generate it for me (like for
This way, there’s no question about how ‘divide’ in Ruby works, whether it’s an integer, float, negative, zero, nil, etc.
1 2 3 def divide(num, den) num/den end
Then with dead simple tests, tests take the form:
Always start with tests. Whenever I’m stuck on a problem, I always ask myself: what is the simplest test I can write? What can I use to help me write this test? A simple set of test to start with:
The implementation would start to look:
1 2 3 def no_divide(num, den) num end
Now with all the basic test and method setup, start making tests that are harder to pass, one that will require a better implementation:
From here, my approach would be to use subtraction in a loop. Division is basically the number of times the denominator can be removed from the numerator.
With a subtraction approach, there needs to be a counter to keep track of how many times the denominator goes into the numerator. So,
1 2 3 4 5 6 7 8 9 def no_divide(num, den) counter = 0 while num - den >= 0 num -= den counter += 1 end counter end
Make some more tests to make sure everything is cool:
1 2 3 4 puts no_divide(4,2) == divide(4,2) puts no_divide(5,2) == divide(5,2) puts no_divide(4,3) == divide(4,3) puts no_divide(4,5) == divide(4,5)
and… that’s it! This is all I’m expecting from a good candidate. I’m not looking for a perfect implementation. I want to see thinking, communicating, and determination to solve a problem, albeit a known problem.
- solve using recursion
- handle negative numbers
This has been the solution to one of my interview questions. I’m not expecting
the world’s best implementation of this question. I definitely want to see
thinking and communication. If you know a better question, send me a quick note:
andrew_at_redgreenrepeat_dot_com. I would love to hear from you.