Just [[currentUser.getNumFreeQuestionsLeft()]] No more free questions left!Upgrade Now
You have a function rand7 that generates a random integer from 1 to 7. Use it to write a function rand5 that generates a random integer from 1 to 5.
rand7 returns each integer with equal probability. rand5 must also return each integer with equal probability.
Your first thought might be to simply take the result of rand7 and take a modulus:
However, this won't give an equal probability for each possible result. We can write out each possible result from rand7 (each of which is equally probable, per the problem statement) and see that some results for rand5 are more likely because they are caused by more results from rand7:
So we see that there are two ways to get 2 and 3, but only one way to get 1, 4, or 5. This makes 2 and 3 twice as likely as the others.
The answer takes worst-case infinite time. However, we can get away with worst-case space. Does your answer have a non-constant space cost? If you're using recursion (and your language doesn't have tail-call optimization), you're potentially incurring a worst-case infinite space cost in the call stack. But replacing your recursion with a loop avoids this.
Worst-case time (we might keep re-rolling forever) and space.
Note that if we weren't worried about the potential space cost (nor the potential stack overflow) of recursion, we could use an arguably-more-readable recursive approach with space cost:
This kind of math is generally outside the scope of a coding interview, but: if you know a bit of number theory you can prove that there exists no solution which is guaranteed to terminate. Hint: it follows from "the fundamental theory of arithmetic".