Skip to main content

Interview question #2

This is related to PHP's array.

An array has number of elements. All elements are integers and unique, which means there is no repetitive integers.
(e.g.) $foo = array(7, 5, 9, 13, 2, 8);

You have to sort the array, provided:
  1. You should scan the elements only once.
  2. You're not allowed to compare the elements when sorting. (i.e., you're not supposed to use any comparison operators)
  3. Sorted resultant array may not be the source array.
How will you do that?

Comments

Anonymous said…
$foo = array(7, 5, 9, 13, 2, 8);

reset($foo);

$bar = array();
$barcount = 0;
while ($foo <> array()) {
$bartemp = min($foo);
$bar[$barcount] = $bartemp;
unset($foo[array_search($bartemp, $foo)]);
$barcount++;
}
print_r($foo); //empty
echo "\n";
print_r($bar); //sorted!
?>


Result:
$foo is Array
(
)

$bar is Array
(
[0] => 2
[1] => 5
[2] => 7
[3] => 8
[4] => 9
[5] => 13
)
Anonymous said…
$bar = $foo;
sort($bar);
reset($bar);
print_r($bar);
For comment#1:You are using min() which is again a comparison function. The answer is not that complex at all.

For comment#2:Are you kidding;)
Anonymous said…
http://us4.php.net/manual/en/language.operators.comparison.php
For comment#4:I meant that you're not allowed to compare the elements in anyway (using operators or functions). Probably I should have worded it better.
Anonymous said…
##Deadeasy##

Popular posts from this blog

I'm Zend (PHP 5) Certified Engineer:-)

As of January 13, 2007, I'm ZCE/ Zend Certified Engineer (PHP 5)

Problems with CakePHP - follow-up

Some people have responded including the Datepicker fame Marc Grabanski . So, this follow-up... First of all, I was not ranting nor complaining; I've just blogged/documented my experience. The common problem most of the people pointed out are that it scales for addons.mozilla.com. Those who have accessed their source code can understand that they've done lot of things and also the site is not database-intensive. You should really create a real database-intensive website to understand what I mean. The other point that been pointed out is about open source and community. Lot of people may not be knowing that it's 2 people pushing it and don't want others to be credited . The generic model or dynamic model idea was originally been from grigri and Marcel . It's hard to be called as open source as only few and sycophants are driving it's direction (I'm not talking about svn access) So, here are my humble checklist before you start shouting at me Did you read a

RTaKeyman Bookmarklet v2.0

RTaKeyman The above bookmarklet is for English to Tamil transcription. This should work like a Input Method Editor on web page forms. The first version I tried didn't work well and so this bookmarklet version. Version 2.0 2004-12-27 1.0 2004-12-12 Usage Make a bookmarklet with the above link. Point to a web page where you want to enter Tamil characters and click the bookmarklet. This will initialize the keyman for that page. Then start typing on the forms; RTaKeyman will transcribe the characters whenever it sees a space, carriage return and tab. To-do Turning on/off keyman (i.e., to enter both Tamil and English). Help and other user interfaces. Credits Sundar for informing the necessity and for being a beta tester