POV-Ray : Newsgroups : povray.off-topic : Unexpected Server Time
31 Oct 2024 14:09:02 EDT (-0400)
  Unexpected (Message 1 to 10 of 30)  
Goto Latest 10 Messages Next 10 Messages >>>
From: Orchid Win7 v1
Subject: Unexpected
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

From: Warp
Subject: Re: Unexpected
Date: 17 Aug 2013 12:56:58
Message: <520fab5a@news.povray.org>
Orchid Win7 v1 <voi### [at] devnull> wrote:
>    int abs(int input)
>    {
>      int value = 0;
>      for (int n = input; n != 0; n++)
>      {
>        value++;
>      }
>      return value;
>    }

Having been a teaching assistant at the computing science lab, I got to
see some pretty weird programs made by students.

For example, you wouldn't believe how complicated calculating the length
of a 2D vector can become in the hands of an inexperienced programmer.
(This should be a literal one-liner, yet even 100 lines of code were
regularly broken just to do this simple thing.)

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

Many C and C++ compilers will actually optimize that to abs(x). I wouldn't
be surprised if a C# compiler wouldn't do the same...

-- 
                                                          - Warp


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: Unexpected
Date: 17 Aug 2013 13:56:37
Message: <520fb955$1@news.povray.org>
On 17/08/2013 05:56 PM, Warp wrote:
> Having been a teaching assistant at the computing science lab, I got to
> see some pretty weird programs made by students.
>
> For example, you wouldn't believe how complicated calculating the length
> of a 2D vector can become in the hands of an inexperienced programmer.
> (This should be a literal one-liner, yet even 100 lines of code were
> regularly broken just to do this simple thing.)

Hmm, yes. I'm just trying to visualise that now... What would I do if I 
had no idea what I'm doing? (I guess the question is, how many of these 
programs ever *worked*?)

>> * return sqrt(x*x); // Evil.
>
> Many C and C++ compilers will actually optimize that to abs(x). I wouldn't
> be surprised if a C# compiler wouldn't do the same...

Perhaps. That doesn't mean this is a good way to achieve that goal though.


Post a reply to this message

From: clipka
Subject: Re: Unexpected
Date: 17 Aug 2013 13:58:36
Message: <520fb9cc$1@news.povray.org>
Am 17.08.2013 11:20, schrieb Orchid Win7 v1:

>    int abs(int input)
>    {

...

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

He should also be prepared for

     return Math.Abs(input);

possibly with an added comment to the effect that the abs(int) function 
should be refactored away at the earliest possible opportunity ;-)


Post a reply to this message

From: Samuel Benge
Subject: Re: Unexpected
Date: 17 Aug 2013 15:20:00
Message: <web.520fcbbc825e3e9f8cb76ff30@news.povray.org>
Orchid Win7 v1 <voi### [at] devnull> wrote:
> I'm sure there must be more evil ways of doing this...)

Here's one (probably not the best) way to do it:

return x*(1-2*(x<0));

Not sure how C# handles types, but in C that would return an int.

Thanks for the laughs, btw :) When Haskell isn't the main subject, you're
actually pretty funny ;)

Sam


Post a reply to this message

From: Samuel Benge
Subject: Re: Unexpected
Date: 17 Aug 2013 15:30:01
Message: <web.520fce4c825e3e9fde4684940@news.povray.org>
"Samuel Benge" <stb### [at] hotmailcom> wrote:
>
> return x*(1-2*(x<0));

Oops, that should be

return x*(1-2*int(x<0));

I just woke up :\

It blows me away that many of the job applicants must be told what an absolute
function is o_O


Post a reply to this message

From: Warp
Subject: Re: Unexpected
Date: 18 Aug 2013 03:50:14
Message: <52107cb6@news.povray.org>
Orchid Win7 v1 <voi### [at] devnull> wrote:
> On 17/08/2013 05:56 PM, Warp wrote:
> > Having been a teaching assistant at the computing science lab, I got to
> > see some pretty weird programs made by students.
> >
> > For example, you wouldn't believe how complicated calculating the length
> > of a 2D vector can become in the hands of an inexperienced programmer.
> > (This should be a literal one-liner, yet even 100 lines of code were
> > regularly broken just to do this simple thing.)

> Hmm, yes. I'm just trying to visualise that now... What would I do if I 
> had no idea what I'm doing? (I guess the question is, how many of these 
> programs ever *worked*?)

Mind you, these people generally knew the pythagorean formula to do the
job (after all, they were university students.) They still managed to
break the 100-line mark.

-- 
                                                          - Warp


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: Unexpected
Date: 18 Aug 2013 04:45:33
Message: <521089ad@news.povray.org>
On 17/08/2013 06:58 PM, clipka wrote:
> He should also be prepared for
>
> return Math.Abs(input);
>
> possibly with an added comment to the effect that the abs(int) function
> should be refactored away at the earliest possible opportunity ;-)

Heh, yeah. I think when an interviewer asks how to implement a standard 
library function, and you call the standard library function, you're 
probably not going to get mad props for that. ;-)


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: Unexpected
Date: 18 Aug 2013 04:47:13
Message: <52108a11$1@news.povray.org>
On 17/08/2013 08:15 PM, Samuel Benge wrote:
> Orchid Win7 v1<voi### [at] devnull>  wrote:
>> I'm sure there must be more evil ways of doing this...)
>
> Here's one (probably not the best) way to do it:
>
> return x*(1-2*(x<0));
>
> Not sure how C# handles types, but in C that would return an int.

Well, I mean, the "obvious" way is

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

But you can phrase that as

   return x ? x : -x;

if you prefer.

> Thanks for the laughs, btw :) When Haskell isn't the main subject, you're
> actually pretty funny ;)

If you wanted to find crazy ways to do this in Haskell, you would have 
some pretty scary options available... ;-)


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: Unexpected
Date: 18 Aug 2013 04:50:49
Message: <52108ae9$1@news.povray.org>
>> Hmm, yes. I'm just trying to visualise that now... What would I do if I
>> had no idea what I'm doing? (I guess the question is, how many of these
>> programs ever *worked*?)
>
> Mind you, these people generally knew the pythagorean formula to do the
> job (after all, they were university students.) They still managed to
> break the 100-line mark.

OK, ouch. Pythagoras and you're *still* getting this wrong??

Mind you, I recall an incident at university where our lecturer wrote 
the Shannon channel theorem on the board, and everyone was like "woah, 
what the heck does 'log' mean?" And the guy was like "oh, I'm sorry, I 
just expected that 3rd year computer science students would know what a 
logarithm is."

It turns out that only two people in the room knew what that actually 
is. One of them was obviously me. Weirdly, the other one was the crazy 
Mancunian rugby lout. It's the first (and last) time I ever saw him say 
something sensible. And it wasn't like he had a vague idea... he seemed 
to actually know what he was talking about. I guess there really *is* 
more to that guy than I first imagined.


Post a reply to this message

Goto Latest 10 Messages Next 10 Messages >>>

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