Elance PHP5 code test (with answers)

February 17, 2011 PHP As Is

Here is the list of problems with correct answers for Elance PHP5 Code Test:

1. recursive xml traversal:

2. (sql) select the second highest id from user table:

3. get checkbox values from POST:

4. get unique array elements:

5. generate random password:

6. split email addresses:

7. the most challenging task (for me). Phone regexps:

8. get longest string in arguments:

9. find maximum in nested array:

10. output all numbers divisable by 8 from 200 to 600 inclusive:

Note, that all answers are correct, but some of them are not optimized, so you may be in top 20% with this, but not higher. If someone would like to post any improvements to the code snippets above – they are welcome:)

Ryan

August 25, 2011 at 7:45 pm

function MaxArray($arr) { $max = 0; foreach($arr as $a) { $tmp = (is_array($a) ? MaxArray($a) : $a; $max = ($tmp > $max) ? $tmp : $max; } return $max; }
Reply

Dorothy

September 4, 2011 at 5:06 am

Very nice, i suggest webmaster can set up a forum, so that we can talk and communicate.
Reply

Andrew

September 4, 2011 at 4:31 pm

function GeneratePassword($length, $chars) { return substr(str_shuffle($chars), 0, $length); }function GetLongestString() { return max(array_map(strlen, func_get_args())); }
Reply

mohit

September 14, 2011 at 8:41 pm

$i=200; while($i%8!=0) { $i++; } while($i<=600) { echo $i; if($i<600) { echo ","; } $i+=8; }
Reply

neo(nickname)

December 21, 2011 at 2:58 pm

hey thank you very much ....... appreciate all of your efforts :)
Reply

pride

December 28, 2011 at 5:06 pm

function GeneratePassword($length, $chars) { return substr(str_shuffle($chars), 0, $length); }pretty good idea,but what if $length's value is greater than strlen($chars) ? you won't get a 10 chars password while $chars='abc'
Reply

admin Post Author

December 28, 2011 at 5:29 pm

nice catch, pride. But then I think Andrew's solution may be improved byreturn substr(str_shuffle(str_repeat($chars,$length)), 0, $length);right?
Reply

kievan

January 4, 2012 at 11:09 pm

Not sure about the GeneratePassword() problem, but I do have a version of the ReadXml(), that I tested to be way more efficient that one in this post. Obviously it is much uglier, and much more rigid, but it runs at least 5 times as fast. Here is the screenshot from KCacheGrind analyzing xDebug profiling output: http://tinypic.com/r/b8k704/5Here is the code:function ReadXml($xmlstr) { $p = xml_parser_create(); xml_parser_set_option ($p, XML_OPTION_CASE_FOLDING, 0); xml_parse_into_struct($p, $xmlstr, $vals); xml_parser_free($p); return ($vals[0]['tag'].PHP_EOL. $vals[1]['tag'].': '.$vals[1]['value'].PHP_EOL. $vals[2]['tag'].': '.$vals[2]['value'].PHP_EOL. $vals[3]['tag'].': '.$vals[3]['value'].PHP_EOL. $vals[4]['tag'].': '.$vals[4]['value'].PHP_EOL); }
Reply

admin Post Author

January 5, 2012 at 8:35 am

Hi kievan, thanks for your solution, hope it helps others!
Reply

kievan

January 5, 2012 at 9:59 pm

I hope so too.And thanx for this great post :) I was able to score 85 for this same test on vWorker.Used your examples while preparing for the test, saved me a good deal of time for sure :)
Reply

dali

January 22, 2012 at 11:18 am

Hi mate, you may loose your account for posting this to public :)
Reply

Tester1

April 23, 2012 at 7:07 am

Sorry for that ugly name, also results worked fine, bud kievan reply about XML DONT works. Thanks dudes :)
Reply

enrique barchiesi

July 4, 2012 at 3:50 pm

The second response is wrong you need to do a double query there. SELECT MAX(id) FROM user WHERE id NOT IN (SELECT MAX(id) FROM user ) Something similar i din´t tested it but it must work.
Reply

Shehan

July 20, 2012 at 6:24 pm

I passed this test on elance and I was top 10%
Reply

Faisal

August 21, 2012 at 8:31 pm

for($i=200;$i<=600;$i++){ if(($i % 8) === 0){ echo $i; if($i != 600){echo ",";} }}
Reply

Faisal

August 22, 2012 at 3:42 am

function GetLongestString(){ global $maxlength; $numofargs = func_num_args(); for($i=0;$i<$numofargs;$i++){ $string = func_get_arg($i); $length = strlen($string); if($maxlength < $length){ $maxlength = $length; } } return $maxlength; }What is the wrong with this code??
Reply

Faisal

August 22, 2012 at 3:45 am

function MaxArray($arr){ $length = count($arr); global $maxValue; for($i=0;$i<$length;$i++){ if(is_int($arr[$i])){ //count maximum value if($maxValue < $arr[$i]){ $maxValue = $arr[$i]; } //end counting } elseif(is_array($arr[$i])){ MaxArray($arr[$i]); } } return $maxValue; }Additionally I don't find any wrong but Elance won't except my code. Do you tell me why?
Reply

Harsh

August 27, 2012 at 10:07 am

Thanks friend really I appreciate your efforts ..
Reply

Sirajus Salayhin

September 17, 2012 at 7:34 pm

SELECT id, name FROM user WHERE id = (SELECT MAX(id) FROM user WHERE id < (SELECT MAX(id) FROM user));
Reply

Marc Donaldson

September 26, 2012 at 3:59 pm

Here is my result to convert the POST values ending with underscore to a sortable array.Based upon 10000 fields it performs 35% faster $res = array(); foreach($_POST as $key => $value) { $res[] = intval(substr($key,strrpos($key,'_') + 1)); } sort($res); $result1 = implode(' ',$res);
Reply

Marc Donaldson

September 26, 2012 at 4:28 pm

Sorry!! Please replace $result1 = with echo
Reply

admin Post Author

September 26, 2012 at 4:56 pm

thanks Marc
Reply

Sirajus Salayhin

October 3, 2012 at 6:47 pm

SELECT DISTINCT(id) FROM `user` WHERE `id`=(SELECT MAX(id) FROM user);
Reply

anonymous

November 5, 2012 at 2:49 pm

Thanks bro you rock :)
Reply

Lokesh Tulsani

November 29, 2012 at 8:48 am

Write a program that outputs the numbers that are divisible by 8 and are between 200 and 600 (inclusive), separated by commas (without spaces or line breaks).<?php $Result = array(); for($i = 200; $i
Reply

jewel

December 1, 2012 at 2:53 pm

super , duper, bumper.
Reply

Faisal

December 20, 2012 at 5:53 am

Your SQL is not perfect:Right one is like this: table name: basic_information roll name total_marks 45 Faisal 90 46 Rasel 89 47 Adit 91 48 Sajib 91SELECT * FROM `basic_information` WHERE `total_marks` = (SELECT DISTINCT `total_marks` FROM `basic_information` ORDER BY `total_marks` DESC LIMIT 1,1);
Reply

Ramesh

March 9, 2013 at 5:52 am

Really aewsome tutorial.....
Reply

GHanta

June 12, 2013 at 10:01 am

you are awesome bro
Reply

iliyas pathan

September 29, 2013 at 12:28 pm

Awesome man..... :)
Reply

Custom Icon Design

February 28, 2014 at 9:23 am

for GeneratePassword ($length,$chars) function$char_length = strlen($chars); should be char_length =strlen($chars) -1;Because rand(0, max) including max. but max element of array is chars[char_length -1], right?
Reply


Leave a Reply

Your email address will not be published