Jump to content
Dev747

Programming interview question

Recommended Posts

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-->C
A-->B
C-->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-->B
B-->C
C-->D

The 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.  

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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>";
 
?>

Share this post


Link to post
Share on other sites

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 by .josh
  • Like 1

Share this post


Link to post
Share on other sites

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. :P

Share this post


Link to post
Share on other sites

<?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;
}

?>

Share this post


Link to post
Share on other sites

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 by Dev747

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
Share on other sites

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 by .josh

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

 I've seen candidates go both ways.

 

Wow. You must have a very interesting interview process.

Share this post


Link to post
Share on other sites

Wow. You must have a very interesting interview process.

What can I say, some people are just more.. adventurous.. than others.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.