Page 1 of 2

PHP OO Interviewee Test Questions

Posted: Mon Mar 01, 2010 10:06 am
by Grim...
I used to post on this forum quite a lot, but it's been ages. So hello to all that remember me. [edit]My last post was in September 2007!

I am interviewing for new code monkeys. The test we currently give code monkeys to fill in is rather old and dull. Here is one of the current questions:

Code: Select all

What is the output of the following code?
<?
function r($p) {
    if ($p==1) {
        return 1;
    } else {
        return $p*r($p-1);
    }
}
echo r(5);
?>
Here is another:

Code: Select all

There are no errors when the following code is executed but is does not function as expected.  What is missing?
 
    $user = array(  1 => array('name' => 'john',  'email' => 'john@tpoll.com'),
            2 => array('name' => 'barry', 'email' => 'barry@tpoll.com'),
            3 => array('name' => 'bob',   'email' => 'bob@tpoll.com'));
 
    //LIST EMAILS
    while (list($id, $user_array) = each($user)) {
        print $user_array['email']."<br>";
    }
    
    
    //LIST NAMES
    while (list($id, $user_array) = each($user)) {
        print $user_array['name']."<br>";
    }
I need various questions, because I want everyone to get some right and some wrong. I've written the easy ones, but writing harder ones is more difficult because something I find hard might be easy to someone else, and vice-versa.

So, if any of you can thing of a hard question about PHP (specifically OO-type stuff) (or MySQL, if you are bored) that can be answered without having to write an essay, I would be interested in seeing them.
Better include the answers, too ;)

Re: PHP OO Interviewee Test Questions

Posted: Mon Mar 01, 2010 10:15 am
by VladSun
Q: How's this pattern called:

Code: Select all

final class HelloWorld{     protected static $_instance;    public $message = 'Hello, world';     private function __construct()    { }     private function __clone()    { }     public static function getInstance()     {      if( self::$_instance === NULL ) {        self::$_instance = new self();      }      return self::$_instance;    }     public function action()     {      echo $this->message;    }} $instance = HelloWorld::getInstance();$instance->action();

Re: PHP OO Interviewee Test Questions

Posted: Mon Mar 01, 2010 1:43 pm
by Christopher
I think patterns would be a good higher-level thing to ask about.

Probably the most basic OO thing to check is use $this->var rather than $var. That is a really common error. And following on that, perhaps a more complex questions about composition. And following that, questions about inheritance -- method overloading, etc.

Re: PHP OO Interviewee Test Questions

Posted: Mon Mar 01, 2010 2:12 pm
by alex.barylski
First you have to define the term code monkey and their expectations/responsibilities/etc.

A code monkey, to me, is someone with a very basic understanding of programming concepts, maybe 1-2 years experience. I wouldn't expect them to know much about OOP, design patterns, practices, etc.

I would test their algorithm/problem solving skills more than anything, their ability to stay consistent and follow requests. I don't want junior developers introducing new classes, changing any interface, etc without my approval, so I could care less about how well they comprehend inhertence, composition, patterns, etc. and more about how well they write code, is it sloppy (inconsistent), give them a problem which would cause duplication at first sight and later be refactored into a member.

Have a conventions document (more pedantic the better) and have them read it. Have them write code and see how well they conform to the document conventions. It's incredible how many people fail this simple, yet important test.

Cheers,
Alex

Re: PHP OO Interviewee Test Questions

Posted: Mon Mar 01, 2010 2:13 pm
by Darhazer
Once we've wrote the following 'test'for OO:

You have a class, that implement interface IDraw, with method draw(), and draws a rectangle.
You have another class, that implements the same interface, with method draw() and draws a circle.
Write a class, that renders rectangle with a X in it.

The expected solution is to create a class, implementing IDraw and rendering X, and than to use Composite pattern, to create the rectangle with X.

Another approach, but not the preferred one (by our team), is to extends the rectangle class.
And if you just create a new class that renders both, without extending the rectangle or use it in a composite, it's considered as a failure. ;)

Re: PHP OO Interviewee Test Questions

Posted: Mon Mar 01, 2010 2:33 pm
by VladSun
OK... A PHP specific OOP question:
Q: What's the output of:

Code: Select all

<?php class MyClass{    private function __call($m, $a)    {        var_dump($m);        var_dump($a);    }}  $var = new MyClass(); $var->sqrt(6);$var->round(2.51);

Re: PHP OO Interviewee Test Questions

Posted: Mon Mar 01, 2010 3:22 pm
by josh
<offtopic>
PCSpectra wrote:A code monkey, to me, is someone with a very basic understanding of programming concepts, maybe 1-2 years experience. I wouldn't expect them to know much about OOP, design patterns, practices, etc.
When job titles include "ninja" or "monkey" it raises a huge red flag to me. I won't lie and say its a deal breaker, but lets just say you offended me and aren't going to get as low of a salary should I accept. I'm a regular person, not a primate. It also shows (to me) you don't know any solid criteria to make hiring decisions on.
</offtopic>

Re: PHP OO Interviewee Test Questions

Posted: Mon Mar 01, 2010 4:28 pm
by AbraCadaver
josh wrote:<offtopic>
PCSpectra wrote:A code monkey, to me, is someone with a very basic understanding of programming concepts, maybe 1-2 years experience. I wouldn't expect them to know much about OOP, design patterns, practices, etc.
When job titles include "ninja" or "monkey" it raises a huge red flag to me. I won't lie and say its a deal breaker, but lets just say you offended me and aren't going to get as low of a salary should I accept. I'm a regular person, not a primate. It also shows (to me) you don't know any solid criteria to make hiring decisions on.
</offtopic>
How about "pirate"?

Re: PHP OO Interviewee Test Questions

Posted: Mon Mar 01, 2010 5:21 pm
by Eran
How about the following test then:
Complete the following sentence:
"I want to be a code ... "
1. Ninja
2. Monkey
3. Pirate
4. Cowboy

Re: PHP OO Interviewee Test Questions

Posted: Mon Mar 01, 2010 7:30 pm
by Benjamin
Why not just view some code samples?

Re: PHP OO Interviewee Test Questions

Posted: Mon Mar 01, 2010 7:42 pm
by josh
AbraCadaver wrote: How about "pirate"?
Does that mean I get to violate DMCA? If so then yeah I'd accept that job.

Actually today I interviewed for a "ninja" position. The questions were like "what is the difference between an abstract class and an interface". Another was "what would you do if someone asked you to do something stupid". My answer to the latter was to the effect of I would try to ask them the goal of doing this "stupid" thing and see if I could suggest a better way, or educate the person on why what they asked was stupid. If they still asked me to do "stupid" stuff daily I'd be gone, is what I told them.

At the end the guy said I'm high on his list but I'm not sure if I even want the position.

Re: PHP OO Interviewee Test Questions

Posted: Tue Mar 02, 2010 3:24 am
by Grim...
VladSun wrote:OK... A PHP specific OOP question:
Q: What's the output of:

Code: Select all

<?php class MyClass{    private function __call($m, $a)    {        var_dump($m);        var_dump($a);    }}  $var = new MyClass(); $var->sqrt(6);$var->round(2.51);
I'm guessing it would be something like "Warning: The magic method __call() must be public" ;)

But that's a good one - cheers. And thanks to everyone else for your input, too.

And sorry to anyone if I offended with the phrase 'code monkey' - I use it to cover people of any level that code for a living (myself included).

Re: PHP OO Interviewee Test Questions

Posted: Tue Mar 02, 2010 3:45 am
by onion2k
Darhazer wrote:Once we've wrote the following 'test'for OO:

You have a class, that implement interface IDraw, with method draw(), and draws a rectangle.
You have another class, that implements the same interface, with method draw() and draws a circle.
Write a class, that renders rectangle with a X in it.

The expected solution is to create a class, implementing IDraw and rendering X, and than to use Composite pattern, to create the rectangle with X.

Another approach, but not the preferred one (by our team), is to extends the rectangle class.
And if you just create a new class that renders both, without extending the rectangle or use it in a composite, it's considered as a failure. ;)
I would expect you to get candidates extending the rectangle object most of the time there simply because there's no reason to go as far as using a composite. Whoever wrote the question isn't very good at spec'ing things.

Don't forget that as much as an interview is for you to find out about a candidate, it's for them to find out about working for your company. If I was given that question and talked through the "expected" answer, I'd be very dubious about accepting a job with you. If you can't make it clear what you want in something that basic what are your specs like for real deliverables?!

Re: PHP OO Interviewee Test Questions

Posted: Tue Mar 02, 2010 4:07 am
by Grim...
I am as interested in the ones they get wrong as the ones they get right, if you see what I mean.

The last two questions on the test are "Do you think employees should be allowed to access Facebook during work hours?" and "Draw a pig." :)

Re: PHP OO Interviewee Test Questions

Posted: Tue Mar 02, 2010 4:51 am
by VladSun
Grim... wrote:I'm guessing it would be something like "Warning: The magic method __call() must be public" ;)
I have no error/warning messages ...

error_reporting(E_ALL|E_STRICT); // error log file is empty
PHP Version 5.2.10

And I really think magic methods should be protected/private - one should not be able to run them directly.