Crazy idea from a recruiter

Nicholas Leippe nick at leippe.com
Wed Mar 27 11:27:47 MDT 2013


On Tue, Mar 26, 2013 at 5:22 PM, Sasha Pachev <sasha at asksasha.com> wrote:
> Maybe we can help him by posting our favorite candidate screening
> challenges. I'll start by posting mine.

Yay, I love puzzles!

>
> - Suppose you are dealing with a parser that does not report line
> numbers for errors - only if parsing was success or error. Lines are
> independent of each other. You are dealing with a sample input
> consisting of 1000000 lines with only one syntax error. How many tries
> will you need to find which line has the error and how will you
> proceed?
>
> - Which language do you think this is written in, and what would be
> the result of execution:
>
>   char* msg;
>   strcpy(msg,"Hello, world!");

This looks like C and if it is C should blow up. You're copying the
string literal to the location pointed to by an uninitialized pointer.

>
> - Assuming you are on a reasonable Unix platform, can you add some
> magic around the code without inserting anything in between the above
> two lines so that it would actually print Hello, world! in spite of
> the egregious error?

Sure. Above it define a macro named 'strcpy' that ignores its first
parameter and simply calls printf("%s", b) with its second parameter
'b'.


>
> - Barring a compiler bug, CPU malfunction or memory corruption is the
> following possible, in which language, and how? You have the code that
> says:
>
>   s.a = 43;
>   printf("s.a=%u\n",s.a);
>
> Those lines result in the following message:
>
> s.a=11

There are likely other possibilities, but some that comes to mind are:
1) this could be a threaded process in C/C++ missing a lock around
these two lines--another thread changed it between them.
2) this could be C++ where s.a is an instance of a class that
overloads operator= to do something non-obvious
3) s.a represents a memory-mapped IO location--reads and writes may
mean very different things


More information about the PLUG mailing list