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

Top Indian web startups

Disclaimer: This list contains some of our team's projects Sites that are useful and usable erail.in - created by a single person and serves useful to quickly find availability in Indian trains ClearTrip - seems to be inspired by many other sites like Vayama , but able to get better UI RediffMail - speed is the big boost Myntra - good idea, but poor UI redBus - good idea, but poor UI and execution Sites from The Times Group are also of good idea, but poorly executed/architected. They clearly try and target the web startup "gap" Sites that are innovative (world's first/never seen before kind) SocialAV - World's first and only antivirus building community (by our team) CSSilize - World's first and only project management tool for slicing works (by our team) jobreapr - World's first and only fixed price job search engine (by our team)

Humble Award - Dr. A.N. Sreevatsan

Dr. A.N. Sreevatsan , ENT specialist located in Adyar, Chennai is one of the gems available in medicines. No assistants in his consulting room and he'd take at least 15mins for every patients. So far I have referred many and all are happy with his approach. Every wannabe doctor should visit him personally to understand his approach. I wish him to be more famous than now. Update (2010-11-21) : Added link to Google Map Keywords : Sreevatsan, Srivatsan, ENT, Adyar, Chennai, Hospital, Doctor

Madurai food Vs Nagercoil food

There's huge cry that Madurai idlis are best. Having stayed in Madurai for 7-years, I can give assurance that they're crap. Madurai foods (especially available in hotels) are of poor hygiene and poor in taste. But, I always like the people--they're friendly and helping. My favorite is always Azad Hotel's (Nagercoil, near Tower Junction) chicken kothu parotta . Nagercoil food like aviyal , samba (red) rice , etc are always good and healthy as they're cooked in coconut oil and with high hygiene--even in hotels.