📜 ⬆️ ⬇️

A collection of practical PHP tasks to prepare for an interview

Hi, Habr!

I would like to share a small collection of practical tasks that may be encountered at the interview.

On the open spaces of Habr and the network there are many collections of questions to prepare for the oral part of the interview, but there was no collection of practical tasks that would be convenient to use for preparation.
')
To start it is enough to clone the collection yourself and start describing the implementation, all the rest is already there. There are ready input data for tasks, there is an FB class that allows you to escape from var_dump () or print_r (). Everything is there, set and you can immediately perform tasks - without too much fuss. I hope someone it will be necessary.


How to use?
For convenience, the tasks are presented by several interfaces folder on Github . Take the ITestSortInterface interface (/ src / Training) as an illustration. From the name it is clear that there are sorting tasks in it:
<?php namespace Training; interface ITestSortInterface { /** *      * *   array(2,5,3,5,6,7,8,9,25,24,18,26,27,28,29,30,31) *     */ public function testSort1($array); /** *       * *   array( * '1'=>array('price'=>10,'count'=>2), * '2'=>array('price'=>5,'count'=>5), * '3'=>array('price'=>8,'count'=>5), * '4'=>array('price'=>12,'count'=>4), * '5'=>array('price'=>8,'count'=>4), * ) *     *  'price' DESC      'count' DESC * array( * '2'=>array('price'=>5,'count'=>2), * '5'=>array('price'=>8,'count'=>4), * '3'=>array('price'=>8,'count'=>5), * '1'=>array('price'=>10,'count'=>5), * '4'=>array('price'=>12,'count'=>4), * ) * */ public function testSort2($array); /** *       *     * array( * array(10,5,3,6), * array(8,2,11,13), * array(9,25,30,18), * array(34,37,38,24) * ) * *      * array( * array(2,5,3,6), * array(8,10,11,13), * array(9,25,24,18), * array(34,37,38,30) * ) * */ public function testSort3($array); } 


To begin the assignment, you must create a class that implements the desired interfaces. I called my class Test:
 <?php //   require_once('loader.php'); use Training\ITestSortInterface; use Training\Data; //  Test   ITestSortInterface class Test implements ITestSortInterface { /** *      * *   array(2,5,3,5,6,7,8,9,25,24,18,26,27,28,29,30,31) *     */ public function testSort1($array) { //    if (!$lenght = count($array)) return $array; //  $x = $y = array(); // ,       ,     . //  -         $i = 1 for ($i = 1; $i < $lenght; $i++) { if ($array[$i] > $array[0]) $x[] = $array[$i]; else $y[] = $array[$i]; } return array_merge($this->testSort1($y), array($array[0]), $this->testSort1($x)); } /** *       * *   array( * '1'=>array('price'=>10,'count'=>2), * '2'=>array('price'=>5,'count'=>5), * '3'=>array('price'=>8,'count'=>5), * '4'=>array('price'=>12,'count'=>4), * '5'=>array('price'=>8,'count'=>4), * ) *     *  'price' DESC      'count' DESC * array( * '2'=>array('price'=>5,'count'=>2), * '5'=>array('price'=>8,'count'=>4), * '3'=>array('price'=>8,'count'=>5), * '1'=>array('price'=>10,'count'=>5), * '4'=>array('price'=>12,'count'=>4), * ) * */ public function testSort2($array) { // TODO: Implement testSort2() method. } /** *       *     * array( * array(10,5,3,6), * array(8,2,11,13), * array(9,25,30,18), * array(34,37,38,24) * ) * *      * array( * array(2,5,3,6), * array(8,10,11,13), * array(9,25,24,18), * array(34,37,38,30) * ) * */ public function testSort3($array) { // TODO: Implement testSort3() method. } } $test = new Test(); /** *  . * \FireDog\FB::info() -       * Data::getData(Data::ARR_SIMPL_INT) -      */ \FireDog\FB::info($test->testSort1(Data::getData(Data::ARR_SIMPL_INT))); 

There are two auxiliary classes \ FireDog \ FB and Training \ Data. (Allow a small remark: Using FB or Data is not necessary - these are helper classes for convenience. However, the browser console seems to me a more convenient alternative to var_dump () or print_r ())
The first one displays debag infu in the browser console:
image
The browser will require the installation of a firephp plugin. There is a plugin for Chrome.
The second class Data, stores the finished data for tasks. To set $ test-> testSort1 () we need an array array (2,5,3,5,6,7,8,9,25,24,18,26,27,28,29,30,31) its we get Data :: getData (Data :: ARR_SIMPL_INT)

The compilation will be replenished and can be used not only to prepare for the interview, but also simply to train PHP programming skills. Send your tasks, the most interesting will be published in the collection.

Collection on Github

PS
The compilation contains only simple tasks that are met during the interview. For this reason, some tasks contain formulations like: “Sort a one-dimensional array on your own” Yes, yes, there is a sort function (), but the context of the interview is important here. If you are assigned the task formulated in exactly this way and you are at the interview, then most likely, such a task has a single goal, namely, to evaluate your algorithmic preparation. I hope this is clear.

Source: https://habr.com/ru/post/266925/


All Articles