Click here to Skip to main content
15,899,474 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hi,
I was given with the below coding challenge in C# in one of the interviews with top tier tech company. Could anyone help me with the optimized solution for this?

/*
The objective of this exercise is to build a road network connecting every pair of cities.Each city should be connected to each other city once.
*/
public class Program
{
/* Your function RoadBuilder should return a list of new roads required to be built,
if the existing roads are given by builtRoads and the total number of
cities is nCities. Roads should not connect cities to themselves.
*/
public static int[][] RoadBuilder(int nCities, int[, ] builtRoads)
{
//implement the function here
return new int[0][];
}

public static void Main()
{
int[, ] test1 = new int[3, 2]{{0, 1}, {1, 2}, {3, 2}};
Console.WriteLine(RoadBuilder(4, test1)); // expected result should be {{0,2}, {0, 3}, {1, 3}}
}
}

What I have tried:

I tried converting the array to list and retrieving the possible combinations from it. But couldn't find an option to remove the already existing connections.
Posted
Updated 21-May-17 22:21pm

We do not do your homework: it is set for a reason.
And interviews are homework - the interviewer needs to know how you think, how work, not me.
Giving you a solution improves your apparent ability falsely, and gives you an advantage over people who can do the job themselves - which is what the company is looking for! We can't support you through your whole career with the company if you get the job, not if yiu get the salary rather than us, anyway, so it would be unfair on everybody. You, in the long run; the genuine applicants; the company.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
Wow, I couldn't disagree more with wet blanket OriginalGriff. I've found one of the best ways to learn is looking directly at a solution and then reverse-engineering it and improving on it. In that spirit, here is a sub-optimal solution that would pass in an interview situation:

C#
public static int[][] RoadBuilder(int nCities, int[,] builtRoads)
{
    // Build lookup of existing connections
    HashSet<string> alreadyBuilt = new HashSet<string>();
    string hereToThere, thereToHere;
    for(int i = 0; i < builtRoads.Length / 2; i++)
    {
        hereToThere = builtRoads[i, 0] + "|" + builtRoads[i, 1];
        thereToHere = builtRoads[i, 1] + "|" + builtRoads[i, 0];
        if(!alreadyBuilt.Contains(hereToThere))
            alreadyBuilt.Add(hereToThere);
        if(!alreadyBuilt.Contains(thereToHere))
            alreadyBuilt.Add(thereToHere);
    }

    // Identify missing roadways
    List<int[]> toBuild = new List<int[]>();
    for(int i = 0; i < nCities; i++)
    {
        for(int j = 0; j < nCities; j++)
        {
            if(i == j) // ignore a city to itself
                continue;
            hereToThere = i.ToString() + "|" + j.ToString();
            thereToHere = j.ToString() + "|" + i.ToString();
            if(!alreadyBuilt.Contains(hereToThere) &&
               !alreadyBuilt.Contains(thereToHere))
            {
                toBuild.Add(new int[2] { i, j });
                alreadyBuilt.Add(thereToHere);
            }
        }
    }

    // Fill formatted results
    int[][] results = new int[toBuild.Count][];
    toBuild.CopyTo(results);
    return results;
}


On a personal note though, I would caution you to stay alert if you went to work there. They want a function that takes a multidimensional array as a parameter and returns a 2d array. In real life that makes no sense, and increases bug probability astronomically. A Java programmer coming to C# would be stumped and even seasoned C# programmers would likely scratch their heads.
 
Share this answer
 
Comments
F-ES Sitecore 22-May-17 4:33am    
I hope he gets the job using your code and becomes your co-worker where you will need to continue to carry him, doing all of his tasks as well as your own.
F-ES Sitecore 22-May-17 5:50am    
If a co-worker was given a task to do and simply asked me how to do it I'd probably look to get them fired, or at a minimum removed from my team. If you are employed it is to do a job, if you can't do that job you shouldn't be employed. Helping people to fraudently obtain a job doesn't help anyone. How would you like it if you went to the doctor and that doctor didn't really have the knowledge to help you, he got other people to sit his exams and answer his interview questions?
F-ES Sitecore 22-May-17 6:14am    
Sometimes you get interview questions to complete in your own time. Or maybe he has a friend also about to take the interview and wants to prepare them in advance.

As for getting people fired I make no apologies for ensuring people are capable of doing the job they were hired for. When you're running a team on a deadline and one person always needs others to do their work for them maybe you'll change your opinion.
F-ES Sitecore 22-May-17 7:21am    
We're not talking about questions or helping...we're talking about someone who has been given a task and doesn't have the ability to do it, to even start it, and instead wants other people to do their job for them. If you can't see the difference between the two then good luck yourself.
F-ES Sitecore 22-May-17 8:18am    
When you've been here a little bit longer you'll grow to know when you're actually helping someone to learn and when you're just doing someone else's job for them. Given three long-term members have one opinion and you have a different one maybe you should consider the fact that those members have experience you do not and that maybe we're right.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900