3 Ways to Loop Without do/do-while/for/foreach/etc...
There are some crazy questions out there to test your knowledge of CS theory that can leave you stumped. It could come from an interviewer who is trying to weed out hobbyists or a student that you’re mentoring who has a tough professor.
One I recently had to think about was this:
Write a program that accepts input for a number from the user then prints a message that number of times WITHOUT using a loop construct, i.e do/do-while/for/foreach explicitly.
There’s one strategy that is likely the one that the questioner is looking for. I’ll give you three so you can stun them…
Strategy #1: Recursion
If an interviewer is asking you, they’re likely looking for you to have a good grasp of how recursion can do this.
public class LoopsWithoutLoops {
private readonly ITestOutputHelper _output;
public LoopsWithoutLoops(ITestOutputHelper output) {
_output = output;
}
[Fact]
public void looping_without_loops_recursion() {
_output.WriteLine("Recursive strategy...");
CountdownRecursive(10);
//Recursive strategy...
//10
//9
//8
//7
//6
//5
//4
//3
//2
//1
//0
}
public void CountdownRecursive(int i) {
if (i < 1) {
_output.WriteLine("0");
return;
}
_output.WriteLine(i.ToString());
CountdownRecursive(i - 1);
}
}
Strategy #2: goto
Here’s another strategy - goto. I’m not saying it’s ideal or preferred or anything, it’s just available…
public class LoopsWithoutLoops {
private readonly ITestOutputHelper _output;
public LoopsWithoutLoops(ITestOutputHelper output) {
_output = output;
}
[Fact]
public void looping_without_loops_goto() {
_output.WriteLine("goto strategy...");
CountdownGoto(10);
//goto strategy...
//10
//9
//8
//7
//6
//5
//4
//3
//2
//1
//0
}
public void CountdownGoto(int i) {
Label:
if (i >= 0) {
_output.WriteLine(i.ToString());
i--;
goto Label;
}
}
}
Strategy #3: Enumerable.Repeat
Finally, there’s LINQ’s Enumerable.Repeat
. Since there’s no explicit call to any do/do-while/for/foreach, I’d say we’ve met the requirements:
public class LoopsWithoutLoops {
private readonly ITestOutputHelper _output;
public LoopsWithoutLoops(ITestOutputHelper output) {
_output = output;
}
[Fact]
public void looping_without_loops_linq() {
_output.WriteLine("LINQ strategy...");
CountdownLINQ(10);
//goto strategy...
//10
//9
//8
//7
//6
//5
//4
//3
//2
//1
//0
}
public void CountdownLINQ(int i) {
var a = i;
Enumerable.Repeat(i, i).Where(j => {
_output.WriteLine(a.ToString());
a--;
return true;
}).ToList();
_output.WriteLine(a.ToString());
}
}
Summary
So there you have it. The question is probably being asked to make sure you’re aware of the recursive strategy so if you’re interviewing, you probably want to use that solution. I’d be pretty impressed if someone listed three ways without hesitation, so feel free to keep them in your pocket.
Thanks for reading! Sign up for my newsletter to get these articles sent right to your inbox!
Related Posts:
- Why Delegates? Why not Call Methods Directly?
- Bad Data, Try/Catch, and Slow Performance
- Singleton Indexer - A Workaround for not having Static Indexers in C#
- Calculating Values using Calculated Values in LINQ
- How to Map Nested JSON Objects to a .NET Dictionary
- Changing a Type within a Class when you Instantiate: An Intro to Generics
- What’s the Point of the ‘dynamic’ Primitive Type?…
- What Needs to Be Disposed?
- Throwing an Invalid Cast Exception?… Look Out for Unboxing