Ayende @ Rahien

Unnatural acts on source code

Code twisters: The functional loop

This might actually be a good interview question:

int level = 10;
Func<int, int> nextLevel = new Func<int, int>(x => x--);
Func<int, bool> loopAgain = new Func<int, bool>(x => x >= 0);
while (loopAgain(level))
{
                // Process Data Logic
               
                // get the next level
                level = nextLevel(level);
}

How many times will this code loop?

With thanks to Jeff Carley for sending me this one.

Comments

Rafal
05/13/2010 09:12 AM by
Rafal

infinite?

Phil Murphy
05/13/2010 09:14 AM by
Phil Murphy

Infinitely, because the use of "x--" returns the value of x before decrementing x (locally), and hence nextLevel( n ) always returns the original value of n?

David a r Kemp
05/13/2010 09:14 AM by
David a r Kemp

Shame this took so long to crack - should have got this straight off from my C++ days.

Kons
05/13/2010 09:15 AM by
Kons

--x should do the trick.

Roma
05/13/2010 09:16 AM by
Roma

x--

rvin100
05/13/2010 09:16 AM by
rvin100

It will be an infinite loop !

Unless you define nextLevel this way :

Func<int, int> nextLevel = new Func<int, int>(x => --x);

nextLevel will always return level...

yew
05/13/2010 09:17 AM by
yew

infinite... should be --x instead of x--

Jason Y
05/13/2010 10:30 AM by
Jason Y

I expect that lambda bodies, like bodies of other methods, use copies of the arguments passed in (copies of value-type arguments or of references, but not copies of objects), so running nextLevel(level) has no side effects outside of itself--i.e., it decrements its local copy of level (x), not level itself. So Rafal is right.

Jason Y
05/13/2010 10:33 AM by
Jason Y

Whoops! Didn't see that level is set to what the lambda expression evaluates to.

Ditto what others said about --x vs. x--.

configurator
05/13/2010 10:53 AM by
configurator

x => x-1 would also work. It's the return value that matters, not the decrement.

AlonE
05/13/2010 11:28 AM by
AlonE

Resharper actually identifies it as a redundant decrement...

Jonty
05/13/2010 11:39 AM by
Jonty

I don't think it would make a good interview question. Good interview questions aren't about catching people out. A good developer might be caught out by some code, but would have unit tests that aren't.

Pointless Code
05/13/2010 12:19 PM by
Pointless Code

I think this is a classic case of unreadable code dressed up as clever code. What's the point?

Bryan
05/13/2010 02:19 PM by
Bryan

@Jonty

IMO the reason this is a good interview question is when you hire a new person, you are going to have them maintaining a bunch of code that they didn't write, and that there is a good chance that they would "have done it differently". That's how this works. So, when the developer is confronted with code that they aren't familiar with, how do they work through the code, and figure out what it does. Do they have critical thinking, and reasoning skills. They don't have to get it right, the goal isn't to see if they get it right, but rather to watch how they reason, and talk through it.

Obviously a unit test, or the debugger could get the answer, and it's good to use tools to solve the problems efficiently, but as developers, we are problem solvers, and you want to exercise those skills in an interview as well as the candidates technical skills.

apalmer
05/13/2010 02:33 PM by
apalmer

Horrible interview question...

Its the kind of thing that will catch 90% of developers out there on the spot, but 90% of the people who got caught would find it within 1 minute with a debugger running, if it came up at work in real life. Plus the whole x--, --x thing is left over from the C/C++ days it really serves no purpose in C# code compared to the much simpler x += 1 construct..

So it filters out people with something that is totally outside of real world usuage, that would easily be resolved in the real world, without testing their true understanding.

Jason Y
05/13/2010 03:31 PM by
Jason Y

I agree that this is a bad question if it is strictly to see how good of a language lawyer the candidate is. However, I can see it being useful for watching how someone goes about solving a problem as Bryan pointed out.

zvolkov
05/13/2010 05:21 PM by
zvolkov

Damn, my comment got flagged as spam :) I just wanted to say: this is how Ayende will stop being Ayende eventually. By embarrassing himself to the point of not making his bold opinions public anymore.

configurator
05/13/2010 05:41 PM by
configurator

I think the more appropriate interview question would be: This code causes an infinite loop. Why?

Dathan
05/13/2010 06:05 PM by
Dathan

To me, ALL interview questions are valid. Whether or not they're GOOD questions has less to do with the question than with how the interviewer uses the answer. In this example, if the interviewer's simply trying to gauge level of factual knowledge, and you get a plus for right and a minus for wrong, then it's a bad question. On the other hand, if the interviewer is using the question to evaluate the developer's thinking processes, it's a perfectly valid question.

Now consider - if that's what the interviewer is doing, everyone who says "You're never going to see that in production code, so it's a terrible question, and you're just prejudicing the interview process against anyone who's never done C" is totally off-base: the candidate who's never done C is AT AN ADVANTAGE, because he gets to show off his thought processes, and the interviewer will be able to accurately evaluate the candidate's problem-solving process. A developer who's got the entire language down cold, though, will just spit out the answer, and will have to sit through another (and maybe another, and another) question before the interviewer can get the information he wants.

Don't discard questions out of hand because you THINK you know the way in which the interviewer is going to use them. Just because the question strikes you as contrived, artificial, or pointless, doesn't mean it really is - the interviewer may just be doing more than you give him credit for.

IObtuse
05/13/2010 06:34 PM by
IObtuse

The purpose of interview questions is this: i want a view into how the candidate thinks and solves problems in a pressure situation.

In that vain, this question is useful from the perspective of how a candidate deals with things like pointless questions or a fool in a 'management' role. Does he/she stay composed?

As a candidate I respect problems like the counterfeit coin, anagrams, etc as they are implicitly respecting me in asking them. Questions like these throw the bozo bit on the employer and raises more red flags than it clears.

Andy
05/14/2010 01:12 AM by
Andy

11 times ?

Bunter
05/15/2010 09:06 PM by
Bunter

I would

Func <int,> nextLevel = new Func <int,> (x => x - 1);

and have no issues with increment/decrement precedence.

SEO
06/21/2010 07:55 AM by
SEO

The purpose of interview questions is this: i want a view into how the candidate thinks and solves problems in a pressure situation.

In that vain, this question is useful from the perspective of how a candidate deals with things like pointless questions or a fool in a 'management' role. Does he/she stay composed?

Comments have been closed on this topic.