
"A good programmer knows how to adapt on the language because what he is capable of is generating an algorithm"
So for today, here is another question that is commonly asked on every interview.
Problem
Part 2 - Print 100 "Hello World" without using any looping code such asfor, while
or do-while
and other similar code.Analysis
For this example, I'll be using a C-type programming syntax.There might be different ways here and one is the manual typing of 100 line codes of
printf
, if you do this then just say good bye to your prospect job. This approach are done by newbies only.What we are about to do is to create a "loop-like" approach by using recursion.
The Code
function loopLike(int n){ if (n > 100) return; else printf("Hello World\n"); return loopLike(n+1); } loopLike(1);
The Algorithm
What we have done is called recursion. We did a loop-like approach by calling the function inside itself and limit it depending on our desired number of iteration.Recursion is a programming technique that allows the programmer to express operations in terms of themselves. Basically it is a way where in one function calls itself or repeat the process.
Nice post :D
ReplyDeleteNapaisip ako dun hahaha. Nice approach!
ReplyDeletethanks! marami pang ganyang question akong naipon.. hahaha
ReplyDeleteyey! tama sagot ko XD
ReplyDeleteAlam mo, lagi ko na aabangan ang "Programmer's Interview" ! \m/
ReplyDeleteI was reading your post and before going further than the problem, so before reading the analysis I decided to try it.
ReplyDeleteFirst of all, I think about doing it recursively, but quickly after opening the command line and launching python, I find out how I can do it :
print 100 * "Hello world!\n"
Would you consider it as using a loop ?
10: PRINT "Hello World"
ReplyDelete20: N := N + 1
30: IF N=100 THEN END ELSE GOTO 10
Funny how the 'noob' approach is more efficient though.
ReplyDelete"noob" :-) LOL, I was programming computers in the late-1970s.
ReplyDeleteI understand your point (as abbreviated by 'noob)', but please know that it's an extremely intentional "GOTO" intended to make a point. The stated requirements failed to specify that GOTO could not be used.
Recursion is clever, and was taught as part of first year Structured Programming course since the 1970s (back when Pascal was The Next Big Thing).
ReplyDeleteSetting exact requirements is far more difficult. LOL. :-)
So how is that *NOT* a loop? Recursion is looping too you know.
ReplyDeleteThat's just a wise ass question, and who wants to work with wise asses if they have a choice? Something the good candidates probably have.
ReplyDeleteI would hope never to find a company that would take such an indirect approach to asking about recursion, but if they did I hope they would also ask about tail-call optimization and stack growth of recursive functions.
ReplyDeleteAlso, for style I would have made the count the argument to loopLike, return when n < 1 and decrement in the recursive call.
What a stupid question. What does it prove? There is no circumstance where this is in any way a useful test for determining the proficiency of a coder. I think I'd prefer to hire someone who DIDN'T come up with such a stupid approach.
ReplyDeleteif would be needed to print 2^x "hello worlds" then solution could be:
ReplyDeletefork(); fork(); fork(); fork(); fork(); fork(); fork();
printf("hello world\n");
wait(NULL);
:-)
What's wrong with
ReplyDeleteprintf("Hello World\nHello World\nHello World\nHello World\nHello World\nHello World\n ... Hello World\n");
?