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

The American College, Madurai - Famous alumni

I'm trying to constantly update this list. It should have been titled "remarkable" or "notable"--instead of "famous". Prof. Kalyani - Social Activist ( link ) Mahendran (birth name: Alexander) - 54PUC?? - film director of Mullum malarum, etc Prof. Solomon Pappiah - 53TAM?? ( link ) Dr. R. ​Prabhakar Vedamanickam - 80ECO?? - Professor, Music Director, Font Creator, known for creativity M. Vallalar IAS - 84ENG?? - Collector of Dindigul District Bala - 83TAM?? - film director of Sethu, etc Vivek (birth name: Vivekanathan) - 78COM59 - comedy actor Ramki - 78?? - actor Thota Tharrani - 64PUC?? - art director Shihan Hussaini - 84ZOO?? - Karate expert and actor Karu. Palaniappan - 90ENG?? - film director of Partiban Kanavu, etc Ram (birth name: Rama Subbu) - 93TAM?? - film director of Katrathu Tamil, etc Shanmugharajan - 99TAM?? - actor (acted in Virumandi, etc Jacob Sahaya Kumar Aruni - 93PHY?? - Chef and VJ

Valen Smith, the English teacher videos are missing!

I vaguely remembered that sometimes around 2009, engVid on YouTube was very popular. I especially liked a teacher by name Valen Smith's videos. She was not only beautiful but also very good at explaining the English usage. I also vaguely remembered to check her channel ValenESL around 2015. But, not sure what happened to her or engVid, all her videos are sadly gone. I have also noted that many people on Twitter were asking the same. Not sure what happened. Some people seem to have reuploaded her videos and YouTube continuously removing them with a note "This video contains content from LearnVid, who has blocked it on copyright grounds." Strangely though, on Facebook, some people have managed to create a fake account using her old channel name ValenESL.