POV-Ray : Newsgroups : povray.off-topic : Unexpected : Unexpected Server Time
28 Jul 2024 16:26:08 EDT (-0400)
  Unexpected  
From: Orchid Win7 v1
Date: 17 Aug 2013 05:20:50
Message: <520f4072$1@news.povray.org>
OK, well I never expected to end up on the *other* side of an interview 
table. o_O

Basically one of the guys had to leave the office suddenly, meaning that 
I was the most senior developer left. (Hey, there's only 4 of us to 
start with!) So, on about twenty minutes' notice, I had to go into an 
interview. Looking like crap, because it's Friday afternoon. :-.

I forget how long the interview was, but my boss apparently made his 
mind up within about five minutes. (Not that he said that at the time, 
of course.)

As part of the interview process, my boss likes to get people to 
implement the absolute function in C#. That is, he got out some paper, 
scribbled

   int abs(int input)
   {

and handed it to the guy. (He usually ends up having to explain what an 
"absolute function" is, but that's fair enough.) The objective is not so 
much to have perfect syntax, but to write something that is logically sound.

On this day, the applicant came up with this:

   int abs(int input)
   {
     int value = 0;
     for (int n = input; n != 0; n++)
     {
       value++;
     }
     return value;
   }

The guy explained how it works, and my boss just stared at him in 
disbelief, and then explained "that's the most bizarre answer I've ever 
seen!"

Perhaps slightly frighteningly, this function *actually works* - for 
negative inputs, at least. If you feed it -7, then the loop counter 
counts upwards from -7 all the way to zero, and "value" at the same time 
counts from 0 up to +7, which is the value returned.

There's a final twist. Because the loop condition is "!= 0" rather than 
"< 0" or similar, if the input is positive to start with, the function 
goes into an infinite loop...

...except that it doesn't. It locks up for a few minutes, AND THEN 
RETURNS -7! O_O

We stared at each other in disbelief, and then realised that the loop 
goes around roughly 2^31 times and then the counter overflows, becoming 
negative. The other counter similarly overflows, eventually resulting in 
the -7 we saw output.

In short, this isn't abs(), this is negate()! And it actually works! (If 
by "works" you accept solutions which are absurdly slow.) Note that if 
you did, say, "n <= 0" then you would have an off-by-one bug resulting 
in the wrong answer. Yet this guy managed (by skill or luck) to avoid 
that pitfall. Without a PC to check it with!

The guy spent so long desperately trying to wrap his brain around how to 
negate a number, that he completely forgot the possibility of the number 
being positive to start with. When we pointed this out, he then spent 
another several minutes trying to figure out how you could perhaps tell 
if a number was positive. He spent ages trying to think up what the name 
of the standard function for that is...

That's pretty special. But my boss told me afterwards it's the most 
bizarre code he's ever seen. *Clearly* he hasn't seen the more 
obfuscated possibilities of logic programming or functional 
programming... I may have to dig up some examples.

(My boss has a standard set of ways to implement abs(). They include:

* if (x < 0) return 0-x; else return x;

* return sqrt(x*x); // Evil.

* return str.Remove('-'); // String munging. Very evil!

* if (x < 0) return x ^ 0x8000; else return x; // DOESN'T ACTUALLY WORK!

I'm sure there must be more evil ways of doing this...)


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.