Web Developer Interview Questions
Some of my favorite hard interview questions for Web Development positions.
PHP Ambiguous Instance
I ran into this line of code from the Smarty function.config_load.php file while trying to compile a PHP project with Quercus: PHP in Java. The Quercus PHP parser was a bit immature and it couldn't parse the line of PHP below. On top of that I couldn't parse the line!
...
if($smarty->force_compile
|| !file_exists($_compile_file)
|| ($smarty->compile_check
&& !$smarty->_is_compiled($_file_path, $_compile_file))) {
// compile config file
if(!is_object($smarty->_conf_obj)) {
require_once SMARTY_DIR . $smarty->config_class . '.class.php';
$smarty->_conf_obj = new $smarty->config_class($_config_dir);
$smarty->_conf_obj->overwrite = $smarty->config_overwrite;
$smarty->_conf_obj->booleanize = $smarty->config_booleanize;
$smarty->_conf_obj->read_hidden = $smarty->config_read_hidden;
$smarty->_conf_obj->fix_newlines = $smarty->config_fix_newlines;
}
$smarty->_conf_obj->set_path($_config_dir);
$_config_vars = array_merge($smarty->_conf_obj->get($_file),
$smarty->_conf_obj->get($_file, $_section));
if(function_exists('var_export')) {
$_output = '';
} else {
$_output = ''\\\'', '\\'=>'\\\\')) . '\'); ?>';
}
$_params = (array('compile_path' => $_compile_file, 'compiled_content' => $_output,
'resource_timestamp' => filemtime($_file_path)));
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.write_compiled_resource.php');
smarty_core_write_compiled_resource($_params, $smarty);
} else {
include($_compile_file);
}
...
I finally figured out what was going on. The statement $smarty->config_class is not accessing a method. The statement is returning a string which is then used to instantiate an object having the class name of the string.
Cartesian Product
In some cases you run into two tables involved in a query that do not have the proper columns to join them together in a 1 to 1 relational mapping. Most of the time this can work but what about when you are aggregating columns in both tables for a summary? This is where you run into problems because one table's rows will be joined to the other table's rows an extra number of times. This extra number of times causes the aggregated column of the one table to be multiplied by the number of extra joins. An example is illustrated below.
Tables
Phone Number table
|
Charge table
|
SQL Query
SELECT
phone.number,
SUM(phone.calls),
SUM(charge.amount)
FROM phone
INNER JOIN charge ON
charge.phoneid = phone.phoneid
GROUP BY
phone.number
Faulty Result
| Number | Calls | Amount |
|---|---|---|
| 555-555-5555 | 8 | 10.00 |
| 666-666-6666 | 12 | 4.31 |
Solutions
SELECT
phone.number,
SUM(phone.calls) / COUNT(*), -- Divide by the number of charge table joins
SUM(charge.amount)
FROM phone
INNER JOIN charge ON
charge.phoneid = phone.phoneid
GROUP BY
phone.number;
-- Also if you have sub-selects
SELECT
phone.number, SUM(phone.calls), (SELECT SUM(charge.amount) FROM charge WHERE charge.phoneid = phone.phoneid)
FROM phone
GROUP BY phone.number;
Intended Result
| Number | Calls | Amount |
|---|---|---|
| 555-555-5555 | 4 | 10.00 |
| 666-666-6666 | 12 | 4.31 |
JavaScript Prototypes
If you ever thought you knew everything about JavaScript then you've never used the Prototype Framework. Did you know functions in JavaScript are also objects? Think of it this way. Functions can have callable methods. The prototype framework leverages this to give you an enhanced object model that includes a class paradigm with inheritance. For most JavaScript developers this goes right over their head. If you want your head spin try to explain the mechanics of this Prototype code snippet below.
Prototype code snippet
var Class = {
create: function() {
var parent = null, properties = $A(arguments);
if (Object.isFunction(properties[0]))
parent = properties.shift();
function klass() {
this.initialize.apply(this, arguments);
}
Object.extend(klass, Class.Methods);
klass.superclass = parent;
klass.subclasses = [];
if (parent) {
var subclass = function() { };
subclass.prototype = parent.prototype;
klass.prototype = new subclass;
parent.subclasses.push(klass);
}
for (var i = 0; i < properties.length; i++)
klass.addMethods(properties[i]);
if (!klass.prototype.initialize)
klass.prototype.initialize = Prototype.emptyFunction;
klass.prototype.constructor = klass;
return klass;
}
};Usage
var Person = Class.create({
initialize: function(name) {
this.name = name;
},
say: function(message) {
return this.name + ': ' + message;
}
});var guy = new Person('Nathan Cassano');
guy.say('JavaScript is prototype-based language');