Have you ever wondered what it would be like to interview for a position at Google or Yahoo?
Well at another blog they shared some pre-interview questions from Yahoo for a PHP job. The only problem is they never provided the answers. So that’s what I’m going to do now.
Yahoo, if you’re watching, please check my answers and if they are right, I can start next week!
Questions 1 – 11
-
Which of the following will NOT add john to the users array?
-
$users[] = 'john';
Successfully adds john to the array -
array_add($users,’john’);
Fails stating Undefined Function array_add() -
array_push($users,‘john’);
Successfully adds john to the array -
$users ||= 'john';
Fails stating Syntax Error
-
-
What’s the difference between sort(), assort() and ksort? Under what circumstances would you use each of these?
-
sort()
Sorts an array in alphabetical order based on the value of each element. The index keys will also be renumbered 0 to length – 1. This is used primarily on arrays where the indexes/keys do not matter. -
assort()
The assort function does not exist, so I am going to assume it should have been typed asort().asort()
Like the sort() function, this sorts the array in alphabetical order based on the value of each element, however, unlike the sort() function, all indexes are maintained, thus it will not renumber them, but rather keep them. This is particularly useful with associate arrays. -
ksort()
Sorts an array in alphabetical order by index/key. This is typically used for associate arrays where you want the keys/indexes to be in alphabetical order.
-
-
What would the following code print to the browser? Why?
- $num = 10;
- function multiply(){
- $num = $num * 10;
- }
- multiply();
- echo $num; // prints 10 to the screen
Since the function does not specify to use $num globally either by using global $num; or by $_GLOBALS['num'] instead of $num, the value remains 10.
-
What is the difference between a reference and a regular variable? How do you pass by reference and why would you want to?
Reference variables pass the address location of the variable instead of the value. So when the variable is changed in the function, it is also changed in the whole application, as now the address points to the new value.
Now a regular variable passes by value, so when the value is changed in the function, it has no affect outside the function.
- $myVariable = "its' value";
- Myfunction(&$myVariable); // Pass by Reference Example
So why would you want to pass by reference? The simple reason, is you want the function to update the value of your variable so even after you are done with the function, the value has been updated accordingly.
-
What functions can you use to add library code to the currently running script?
This is another question where the interpretation could completely hit or miss the question. My first thought was class libraries written in PHP, so include(), include_once(), require(), and require_once() came to mind. However, you can also include COM objects and .NET libraries. By utilizing the com_load and the dotnet_load respectively you can incorporate COM objects and .NET libraries into your PHP code, thus anytime you see “library code” in a question, make sure you remember these two functions.
-
What is the difference between foo() & @foo()?
foo() executes the function and any parse/syntax/thrown errors will be displayed on the page.
@foo() will mask any parse/syntax/thrown errors as it executes.
You will commonly find most applications use @mysql_connect() to hide mysql errors or @mysql_query. However, I feel that approach is significantly flawed as you should never hide errors, rather you should manage them accordingly and if you can, fix them.
-
How do you debug a PHP application?
This isn’t something I do often, as it is a pain in the butt to setup in Linux and I have tried numerous debuggers. However, I will point out one that has been getting quite a bit of attention lately.
PHP – Advanced PHP Debugger or PHP-APD. First you have to install it by running:
pear install apdOnce installed, start the trace by placing the following code at the beginning of your script:
apd_set_pprof_trace();Then once you have executed your script, look at the log in apd.dumpdir.
You can even use the pprofp command to format the data as in:
pprofp -R /tmp/pprof.22141.0For more information see http://us.php.net/manual/en/ref.apd.php
-
What does === do? What’s an example of something that will give true for ‘==’, but not ‘===’?
The === operator is used for functions that can return a Boolean false and that may also return a non-Boolean value which evaluates to false. Such functions would be strpos and strrpos.
I am having a hard time with the second portion, as I am able to come up with scenarios where ‘==’ will be false and ‘===’ would come out true, but it’s hard to think of the opposite. So here is the example I came up with:
- if (strpos("abc", "a") == true)
- {
- // this does not get hit, since "a" is in the 0 index position, it returns false.
- }
- if (strpos("abc", "a") === true)
- {
- // this does get hit as the === ensures this is treated as non-boolean.
- }
-
How would you declare a class named “myclass” with no methods or properties?
- class myclass
- {
- }
-
How would you create an object, which is an instance of “myclass”?
- $obj = new myclass();
It doesn’t get any easier than this.
-
How do you access and set properties of a class from within the class?
You use the $this->PropertyName syntax.
- class myclass
- {
- private $propertyName;
- public function __construct()
- {
- $this->propertyName = "value";
- }
- }
There you have it! The answers to questions 1 through 11. Stay tuned for Part 2 where I tackle 12 through 22. If you have any questions, need further explanation, or just want to contribute to any of the above questions, comments are always welcome.

August 13th, 2007 at 9:23 pm
This articles rock!
August 14th, 2007 at 7:01 am
Great post. Thanks a lot!:)
August 14th, 2007 at 8:03 am
Yeah, nice work Matt, I don’t think I would have got the job. But, I could have said, “hey, I work well with people!”
August 14th, 2007 at 9:15 am
Dian/Alex,
Glad you enjoyed it. Golgotha, you are definitely much more of a people person than programmer, but if you spend countless hours with PHP or any language for that matter, it’s amazing how much you never realize you knew until you are tested with such an interview.
Keep an eye out for Part 2, as I was really pleased with myself, I knew which version of PHP introduced GD as “built-in” without looking it up. Anyone care to guess before I release the answer?
August 14th, 2007 at 3:40 pm
Regarding Question 8:
“1″ == 1 is true
“1″ === 1 is false
August 14th, 2007 at 4:54 pm
Roberto Lupi, excellent. I have no idea why I couldn’t think up an example, must have been a mental roadblock, but thanks for pointing one out.
August 19th, 2007 at 3:20 pm
I’ll tell you from experience that the technical interview is usually only a tiny part of the interview process with these companies, and the theoretical stuff is much harder. You essentially need to have taken discrete math and algorithms/graph theory courses at college level to get through the design & analysis questions. At Google, these came first, with the technical interview (programming questions like these for whatever language the job you’re applying for uses) coming after.
August 23rd, 2007 at 5:24 pm
You, sir, did not answer, “How do you pass by reference…”!
August 23rd, 2007 at 10:33 pm
Here are some sample questions from GLAT or Google Labs Aptitude Test:
1. Solve this cryptic equation, realizing of course that values for M and E could be interchanged. No leading zeros are allowed.
WWWDOT – GOOGLE = DOTCOM
2. Write a haiku describing possible methods for predicting search traffic seasonality.
3. What number comes next in the sequence:
10, 9, 60, 90, 70, 66
a) 96
b) 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
c) Either of the above
d) None of the above
Source: The Google Story by David A. Vise
August 27th, 2007 at 7:04 am
[...] you read ‘Yahoo Job Interview Questions: Part 1′ because you were interested in the answers to their questions, then you will definitely be [...]
August 29th, 2007 at 4:15 am
Regarding question 8 :
$var=NULL;
$var==0 – true;
$var===0 – false;
August 29th, 2007 at 7:08 am
Matt Wilkin’s Blog: Yahoo Job Interview Questions (and Answers) Parts 1 & 2…
…
August 29th, 2007 at 11:00 am
Nice, but what is an “associate array”? I think you mean associative.
September 2nd, 2007 at 12:32 pm
‘===’ compares the types of the operands first, if they differ it will return ‘false, if they are equal, it will compare their values.
October 17th, 2007 at 4:12 am
What functions can you use to add library code to the currently running script?
While com_load() and dotnet_load() are specific for com objects and dotner libraries, the dl() function http://in2.php.net/manual/en/function.dl.php can load any .so or .dll file
October 18th, 2007 at 12:04 pm
[...] Check it out! While looking through the blogosphere we stumbled on an interesting post today.Here’s a quick excerpt [...]
March 20th, 2008 at 6:40 am
There are few more interviews by yahoo at this blog.
Yahoo Interview – 1
Yahoo Interview – 2
August 11th, 2008 at 11:45 am
Wow….these were quite useful. I also checked out another site with a huge set of questions. Try taking a peek before your interveiw:-
http://www.kanbal.com/index.php?/PHP/php-interview-questions-set-1.html
I found it useful
November 10th, 2008 at 9:14 am
Explanation on == and === .
1) Operator == is used to check two objects are same or not.
So if 2 objects have same property values and are instances of same class then it returns true else false .
2)operator === is used to check if 2 variables actually refer to same object instance.
ex:- class abc { }
$a= new abc();
$b=&$a;
then it returns true .
3) example of something that will give true for == and not for === is
clone() function.
cont …. above program
$c= clone($a);
then ($a==$b) returns true ,
but ($a===$b) returns false.
July 12th, 2009 at 12:37 pm
Great post! Yahoo sure has some difficult questions!
July 24th, 2009 at 9:35 am
Greate Meterials…. Thanks
Finding hard to solve ….
3. What number comes next in the sequence:
10, 9, 60, 90, 70, 66
Confused about the answer “d) None of the above”
What do you say “Yahoo Job Interview Questions: Part 2″?
July 8th, 2010 at 9:58 am
Hey thanks a lot for sharing useful interview questions….. which will be very helpful while attending the interviews…..
glad i found ur site…really a very helpful site…..
by the way check out my collection of php interview questions from here: php interview questions