需要有关C#中2D阵列的面试挑战问题的帮助 [英] Need help on interview challenge question on 2D arrays in C#

查看:63
本文介绍了需要有关C#中2D阵列的面试挑战问题的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在与顶级科技公司的一次采访中,我接受了C#中的以下编码挑战。任何人都可以帮我找到优化的解决方案吗?



/ *

本练习的目的是建立一个连接每个人的道路网络一对城市。每个城市应该相互连接一次。

* /

公共课程

{

/ *您的功能RoadBuilder应该返回需要建造的新道路列表,如果现有道路由builtRoads给出,则需要返回


城市的总数是nCities。道路不应该将城市连接到自己。

* /

public static int [] [] RoadBuilder(int nCities,int [,] builtRoads)

{

//在这里实现这个功能

返回新的int [0] [];

}



public static void Main()

{

int [,] test1 = new int [3,2] {{0,1 },{1,2},{3,2}};

Console.WriteLine(RoadBuilder(4,test1)); //期望的结果应该是{{0,2},{0,3},{1,3}}

}

}



我尝试过:



我尝试将数组转换为列表并从中检索可能的组合它。但是找不到删除现有连接的选项。

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.

推荐答案

我们不做你的功课:设置是有原因的。

面试是家庭作业 - 面试官需要知道如何思考,如何工作,而不是我。

给你一个解决方案可以错误地提高你的表现能力,并且比那些能够自己完成工作的人更有优势 - 这正是公司所寻求的!如果你得到这份工作,我们无法支持你在公司的整个职业生涯,不管你是否得到工资而不是我们,所以这对每个人都是不公平的。你,从长远来看;真正的申请人;公司。



亲自尝试一下,你会发现它并不像你想象的那么难!



如果您遇到特定问题,请询问相关问题,我们会尽力提供帮助。但是我们不会为你做这一切!
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!


哇,我不能不同意湿毯OriginalGriff。我发现最好的学习方法之一就是直接查看解决方案,然后对其进行逆向工程并对其进行改进。本着这种精神,这是一个次优的解决方案,可以通过面试的情况:



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:

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





但就个人而言,如果你去上班,我会提醒你保持警惕那里。他们想要一个函数,它将多维数组作为参数并返回一个二维数组。在现实生活中毫无意义,并且在天文数字上增加了错误概率。一个来到C#的Java程序员会感到难过,即使是经验丰富的C#程序员也可能会头疼。



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.


这篇关于需要有关C#中2D阵列的面试挑战问题的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆