As I have said from the previous Programmer Interview 101 post, in every job interview for a programmer, the ability on how to formulate an algorithm on a short span of time is always tested.

"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 as `for, 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.

1. picogenkaku5/13/12, 3:02 PM

Nice post :D

2. Napaisip ako dun hahaha. Nice approach!

3. thanks! marami pang ganyang question akong naipon.. hahaha

4. yey! tama sagot ko XD

5. Alam mo, lagi ko na aabangan ang "Programmer's Interview" ! \m/

6. I was reading your post and before going further than the problem, so before reading the analysis I decided to try it.
First 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 ?

7. 10: PRINT "Hello World"
20: N := N + 1
30: IF N=100 THEN END ELSE GOTO 10

8. Funny how the 'noob' approach is more efficient though.

9. "noob" :-) LOL, I was programming computers in the late-1970s.

I 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.

10. 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).

Setting exact requirements is far more difficult. LOL. :-)

11. So how is that *NOT* a loop? Recursion is looping too you know.

12. That'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.

13. I 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.

Also, for style I would have made the count the argument to loopLike, return when n < 1 and decrement in the recursive call.

14. Ian Littlewood10/7/12, 11:44 AM

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.

15. if would be needed to print 2^x "hello worlds" then solution could be:

fork(); fork(); fork(); fork(); fork(); fork(); fork();
printf("hello world\n");
wait(NULL);

:-)

16. Not Relevant12/25/12, 6:06 PM

What's wrong with

printf("Hello World\nHello World\nHello World\nHello World\nHello World\nHello World\n ... Hello World\n");

?