Dev747 Posted August 20, 2015 Share Posted August 20, 2015 I'm a newbie to PHP and recently had a interview which required to write code in PHP for the following problem and which I was unable to do so. Please help with possible solutions (preferably with codes). Problem :A person wants to travel from one point to another where A is the source point and D is the destination point. The inputs to the problem consisted of paths to be traversed while traversing from source to destination as follows :B-->CA-->BC-->D( The input paths are to be considered in these orders only)The aim is to give correct sequence of paths which is as follows :A-->BB-->CC-->DThe interviewer gave hint that in the correct sequence of paths ( A-->B , B-->C , C-->D ), the end point of the 1st path is the start point of the 2nd path and the end point of the 2nd path is the start point of the 3rd path. He also stated that it can be achieved with associative arrays in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/297875-programming-interview-question/ Share on other sites More sharing options...
ginerjm Posted August 20, 2015 Share Posted August 20, 2015 (edited) I Edited August 20, 2015 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/297875-programming-interview-question/#findComment-1519341 Share on other sites More sharing options...
ginerjm Posted August 20, 2015 Share Posted August 20, 2015 If you create the an array with the source points being the element keys and the dest as the corresponding value, you can then use array notation to get what you need. Start by finding the value for the array key that matches your starting point. Then take its value and use it as the key of the next path and continue until the value matches the destination you want. Kinda pointless now since you most likely failed the interview, eh? Quote Link to comment https://forums.phpfreaks.com/topic/297875-programming-interview-question/#findComment-1519342 Share on other sites More sharing options...
Psycho Posted August 20, 2015 Share Posted August 20, 2015 The following solution protects against an item not existing in the list, but does not contain logic to prevent an infinite loop if the values were to contain such a problem <?php $paths = array( 'A' => 'B', 'B' => 'C', 'C' => 'D', 'D' => 'A' ); function getPaths($fullStart, $fullEnd, $paths) { $pathListAry = array(); //Return array $pathStart = $fullStart; //function var $pathEnd = false; //function var while($fullEnd != $pathEnd) //Run loop until full end is found { if(!isset($paths[$pathStart])) { echo "Error: No path starting with $pathStart"; return false; } $pathEnd = $paths[$pathStart]; $pathListAry[$pathStart] = $pathEnd; //Set start to current path end point $pathStart = $pathEnd; } return $pathListAry; } //Run solution $start = 'A'; $end = 'D'; $thePaths = getPaths($start, $end, $paths); //Output results echo "Starting from $start and ending at $end : <br><br>\n"; echo "<pre>" . print_r($thePaths, true) . "</pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/297875-programming-interview-question/#findComment-1519343 Share on other sites More sharing options...
.josh Posted August 20, 2015 Share Posted August 20, 2015 (edited) or at face value you could could go the "cheap GIGO answer" route, accept input as full string and just sort it: $points = array( 'B-->C', 'A-->B', 'C-->D' ); sort($points); print_r($points); /* output: Array ( [0] => A-->B [1] => B-->C [2] => C-->D ) */ Or with an associative array: <?php $points = array( 'B'=>'C', 'A'=>'B', 'C'=>'D' ); asort($points); print_r($points); /* output: Array ( [A] => B [B] => C [C] => D ) */ Edited August 20, 2015 by .josh 1 Quote Link to comment https://forums.phpfreaks.com/topic/297875-programming-interview-question/#findComment-1519345 Share on other sites More sharing options...
scootstah Posted August 20, 2015 Share Posted August 20, 2015 or at face value you could could go the "cheap GIGO answer" route, accept input as full string and just sort it: $points = array( 'B-->C', 'A-->B', 'C-->D' ); sort($points); print_r($points); /* output: Array ( [0] => A-->B [1] => B-->C [2] => C-->D ) */ That's either going to work for ingenuity, or not work for cheating. Quote Link to comment https://forums.phpfreaks.com/topic/297875-programming-interview-question/#findComment-1519346 Share on other sites More sharing options...
Barand Posted August 20, 2015 Share Posted August 20, 2015 <?php $paths = [ 'B' => 'C', 'A' => 'B', 'C' => 'D' ]; $orig = 'A'; $dest = 'D'; $start = $orig; $end = ''; while ($end != $dest) { $end = $paths[$start]; echo "$start —> $end<br>"; $start = $end; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/297875-programming-interview-question/#findComment-1519347 Share on other sites More sharing options...
mike_bike_kite Posted August 21, 2015 Share Posted August 21, 2015 Can I ask why you were at an interview for a programming job for PHP in the first place? Were you just feeling lucky? Quote Link to comment https://forums.phpfreaks.com/topic/297875-programming-interview-question/#findComment-1519363 Share on other sites More sharing options...
Dev747 Posted August 21, 2015 Author Share Posted August 21, 2015 (edited) Thank you guys for the help. Actually I forgot to mention that the solution was supposed to be applicable for any possible set of inputs. In such case we cannot use ‘sort’ function. For example if B is source and C is destination and the correct sequence of paths are B->A , A->D, D->C which is to be determine from given set of inputs ( path sequences in random order ).I think the logic here would be that we have to start from source and then the end point of the 1st path is the start point of the 2nd path and the end point of the 2nd path is the start point of the 3rd path. Please correct me if I'm wrong. Edited August 21, 2015 by Dev747 Quote Link to comment https://forums.phpfreaks.com/topic/297875-programming-interview-question/#findComment-1519370 Share on other sites More sharing options...
Psycho Posted August 21, 2015 Share Posted August 21, 2015 I think the logic here would be that we have to start from source and then the end point of the 1st path is the start point of the 2nd path and the end point of the 2nd path is the start point of the 3rd path. Please correct me if I'm wrong. Well, scootstah's 'creative' solution aside, both mine and Barands's solutions would work. If you review those two you would answer your question above. Quote Link to comment https://forums.phpfreaks.com/topic/297875-programming-interview-question/#findComment-1519384 Share on other sites More sharing options...
Dev747 Posted August 21, 2015 Author Share Posted August 21, 2015 Well, scootstah's 'creative' solution aside, both mine and Barands's solutions would work. If you review those two you would answer your question above. Of course those solution will work. Actually I was concerned about the solutions given using 'sort' function. By the way thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/297875-programming-interview-question/#findComment-1519397 Share on other sites More sharing options...
scootstah Posted August 21, 2015 Share Posted August 21, 2015 Well, scootstah's 'creative' solution aside That was from .josh Quote Link to comment https://forums.phpfreaks.com/topic/297875-programming-interview-question/#findComment-1519399 Share on other sites More sharing options...
.josh Posted August 21, 2015 Share Posted August 21, 2015 (edited) Well you know, I did preface it with: or at face value you could could go the "cheap GIGO answer" route, accept input as full string and just sort it Edited August 21, 2015 by .josh Quote Link to comment https://forums.phpfreaks.com/topic/297875-programming-interview-question/#findComment-1519404 Share on other sites More sharing options...
.josh Posted August 21, 2015 Share Posted August 21, 2015 You know how like if a question was something like "write some code to output 1 to 100" and you just put echo "1 to 100"; It's totally not what they really wanted, but technically not wrong. So maybe they say "wow, thanks for wasting our time.." or maybe they say "wow! this dude has a sense of humor, we'll hire him!". I've gotten answers like that from job candidates, and it has indeed influenced me giving them the thumbs up. For example if I could tell from their other answers that they know their shit, then I'm down for seeing answers like this. But if I can tell from their other answers they don't know wtf they are doing.. I'll probably think they really thought that's what the ask was, and I'd facepalm. As someone who is part of the hiring process for devs, I've seen candidates go both ways. Anyways, TL;DR: It wasn't meant to be a serious answer, and I prefaced it as such! Quote Link to comment https://forums.phpfreaks.com/topic/297875-programming-interview-question/#findComment-1519405 Share on other sites More sharing options...
scootstah Posted August 22, 2015 Share Posted August 22, 2015 Well, I liked it. As someone who has been on both sides of the coin, I think questions like this are stupid and a waste of time. The person who wrote the question is probably only looking for one specific answer, to a problem that has a multitude of different solutions. I don't think these types of questions allow you to express yourself and demonstrate your experience. It just shows that you are good at interviews. Quote Link to comment https://forums.phpfreaks.com/topic/297875-programming-interview-question/#findComment-1519407 Share on other sites More sharing options...
Psycho Posted August 22, 2015 Share Posted August 22, 2015 I've seen candidates go both ways. Wow. You must have a very interesting interview process. Quote Link to comment https://forums.phpfreaks.com/topic/297875-programming-interview-question/#findComment-1519408 Share on other sites More sharing options...
.josh Posted August 22, 2015 Share Posted August 22, 2015 Wow. You must have a very interesting interview process. What can I say, some people are just more.. adventurous.. than others. Quote Link to comment https://forums.phpfreaks.com/topic/297875-programming-interview-question/#findComment-1519412 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.