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.
jcm said…
Without any comparison, though it's quite dumb :-)
$foo = array(7, 5, 9, 13, 2, 8);
$newfoo = array();
$c = count($foo);
$i = 0;
while (count($newfoo) < $c) {
if (in_array($i, $foo)) $newfoo[] = $i;
$i++;
}
var_dump($newfoo);
?>
91 said…
##Deadeasy##

Popular posts from this blog

Using Sodexo Meal Pass Card in BigBasket.com - How-to

How to Use Sodexo Meal Pass Card in Bigbasket.com Sodexo started to issue Smart Card similar to Credit Card--instead of their usual paper based vouchers.

I was using Sodexo vouchers for longtime, but it was a big letdown when I can't get some of my unused vouchers renewed. And so, I was somehow happy with their new Meal Pass Card. But, again, it was a big letdown as I can't use it with BigBasket--where I usually use most of my meal coupon vouchers.
SMS message from Sodexo on December 20, 2017 1:18 PM came as a big surprise, as I was waiting for this for longtime:
Great News! Pay with your Sodexo Meal Card for food items ordered from Big Basket. Choose Sodexo in the payment section & pay on delivery.

And so, this month, I tried to use my Sodexo Meal Pass Card in BigBasket.

BigBasket website had not much information; I thought that I have to swipe my Meal Pass Card. Delivery person also didn't have much information; initially he was rejecting my Card! I tried to contact …

Who is S.P. Udayakumar, (Koodankulam anti-nuclear) activist of the year

I greatly admired S.P. Udayakumar  (Koodankulam anti-nuclear activist) for his calm and composed response; it is usually not easy to reply moronic Narayanasamy and moronic Ponraj (advisor of Abdul Kalam) who're always blabbering irrationally. By wrongly judging from his sober and humble look, I'd thought that he's an illiterate villager from Koodankulam. But, recently I came to know that he's studied in USA and worked as a professor there. My respect for him greatly increased when I compared him with other exhibitionist NRIs.

Here are the details that I collected...

Name: S.P. Udayakumar
Age: 50
Native: Nagercoil
Family: Wife Meera running SACCER primary and middle school, 2-sons

Education

M.A. (Peace Studies) from Notre Dame University in the United States (1990)Ph.D. (Political Science) from the University of Hawaii (1996)
(Past) Occupation 

Research fellow at Kirwan InstituteProfessor at Governor's School of Public Issues and the Future of New Jersey at Monmouth Univer…

Converting PSD with PHP/ImageMagick

After seeing feature rich options in Imagick PECL extension at Mikko Koppanen's (the author) website and also impressed with ImageMagick's features, I have decided to use it for the PSD to XHTML conversion website that I'm architecting and managing.

Since, the team wants programming help for converting PSD images, I have tried it (documentation is sparse on PSD handling)

Converting PSD to PNG/JPEG/etc
Note that, flattenImages() is needed for layered/multi-page PSD file.


<?php
$im = new Imagick('test.psd');
$im->flattenImages();
$im->setImageFormat('png');
$im->writeImage('test.png');
?>



Extracting PSD layers
One by one


<?php
$im = new Imagick('test.psd');
$im->setImageFormat('png');
for ($i = 0, $num_layers = $im->getNumberImages(); $i < $num_layers; ++$i) {
$im->setImageIndex($i);
$im->writeImage('layer' . $i . '.png');
}
?>


Note that, there is a better version below

In a single call with write…