"Implement multiplication without using loops."
"Uh, okay. What do you mean by loops?"
"Don't use a conditional loop."
"What do you mean by a conditional loop?"
"Oh, you know, the standard definition."
Time passes.
"I'm stuck. What is the answer?"
"Oh, you just have a loop on b dividing it by 2 using shift operators until it is zero."
"Wait a minute, you said you couldn't use loops."
"Did I? Ah, well."
Hackerrank/leetcode exercises are written the same way. So many times that a problem asks "Output the indexes of two numbers in the array such that their sum is K" and you write your code and the website says "INCORRECT! You said [3,6] but the right answer was [6,3]". Addition is commutative! The two are equal! And both right!
That page is both hilarious and sad at the same time. Hilarious because the second "solution" clearly has a loop, and sad because sites like those don't really help anyone. Some of the pages on that site are downright WTFs:
Can you elaborate on this? I understand the shifting but didn't understand the "loop unrolling fixed iteration part"
Uh, ok:
a * b = b != 0 ? a/(1/b) : 0
Done.
EDIT: handle division by zero.
I would argue that a loop constructs an answer, while recursion deconstructs input.
There's a duality, so you can port algorithms between the two models, but there is an actual distinction.
I never understood why anyone would ask say DP questions in an interview. We don’t use that in 99.9% of software (I’ve never once in my career found a use case). What you’re really testing is whether or not the candidate had enough time to refresh that material. Worse still, it involves a very rigid solution pattern that’s over-specified to that class of problem and tells you nothing about what you want to know: can a candidate synthesize concepts to devise a solution to problems we actually encounter?
"Replace each element of array with product of every other element without using / operator"
use the / operator?
My solution would involve summing the log() of the values in the array.