Skip to content

Instantly share code, notes, and snippets.

@Andrewpk
Last active March 31, 2016 05:45
Show Gist options
  • Save Andrewpk/9bfae5b6cd7d6e6bf121699c8b1de659 to your computer and use it in GitHub Desktop.
Save Andrewpk/9bfae5b6cd7d6e6bf121699c8b1de659 to your computer and use it in GitHub Desktop.
Silly craptastic interview question
<?php
/**
* I was given a really silly sorting question to write out in pseudo code once after spending 9 hours traveling
* to the HQ of the employer and running on about 3 hours of sleep.
* I figured I'd do poorly, and I did, but I explained my method.
* I just wasn't able to get the code on the board without constantly erasing and not having a clear head so I was
* passed over for another candidate.
* I named this interviewLulz because I find it lulzworthy when an employer uses sorting questions as their test when
* the sorting requested is:
* A) Ridiculous.
* B) Library fodder - the actual facilities of sorting a set have so little to do with the day-to-day job.
*
* This is why when I interview someone, I bring sanitized/safe for disclosure code from our actual codebase and
* have the prospective employee work on actual problems or ask them how they'd improve something.
* At my current employer - they have them pair with us.
* Whiteboard coding and 'trick' coding questions during interviews are for the old guard.
*
*
* Lately I've been trying to rid myself of past interview anxiety as I look at the job market and try to decide if I
* should move on, but the fact that I never got the code down for this interview still bugged me.
*
* So, since that interview in question irked me so, I figured I'd write a solution to it to get it off my back...
* And then I wrote a solution for a slight modification to the initial problem they posed to me.
* And since it's lulzworthy - I did both in about 10 minutes total in some real shitty PHP :D
* Hopefully my mind will be at peace now.
*/
$randomNumbers = [];
//"array could be any length of non-unique numbers"
for ($i = 0; $i < 20; $i++) {
$randomNumbers[] = mt_rand(0, 20); //I chose this range just for easier reading
}
$totalItems = count($randomNumbers);
echo "Our numbers to be sorted:\n";
echo join(', ', $randomNumbers) . "\n";
sort($randomNumbers, SORT_NUMERIC); //SORT!
$lowerHalf = array_splice($randomNumbers, round($totalItems/2));
rsort($lowerHalf, SORT_NUMERIC); //ANOTHER SORT! lulz
$compiledArray = [];
for ($i = 0; $i < $totalItems; $i++) {
if ($i % 2) {
$compiledArray[] = array_shift($lowerHalf);
} else {
$compiledArray[] = array_shift($randomNumbers);
}
}
echo "\nOMGZ Array sorted a >= b <= c modified interview method:\n";
for ($i = 0; $i < $totalItems; $i++) {
if ($i < ($totalItems - 1)) {
$signageString = ($i % 2) ? " >= " : " <= "; //wat
echo $compiledArray[$i] . $signageString;
} else {
echo $compiledArray[$i] . "\n";
}
}
rsort($compiledArray, SORT_NUMERIC); //WHY NOT A SORT?
$randomNumbers = $compiledArray; //wat
$compiledArray = []; //OH NO REUSE
$lowerHalf = array_splice($randomNumbers, round($totalItems/2)); //didn't we do this already
for ($i = 0; $i < $totalItems; $i++) { //I heard you like for loops
if ($i % 2) { //we aren't calculating tips commonnnnnn
$compiledArray[] = array_shift($lowerHalf);
} else {
$compiledArray[] = array_shift($randomNumbers);
}
}
echo "OMGZ Array sorted a <= b >= c interview method:\n";
for ($i = 0; $i < $totalItems; $i++) {
if ($i < $totalItems - 1) {
$signageString = ($i % 2) ? " <= " : " >= "; //wat
echo $compiledArray[$i] . $signageString;
} else {
echo $compiledArray[$i] . "\n\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment