前端面试的作品示例_如何回答任何技术面试问题-包括示例

前端面试的作品示例

Technical interviews can be extremely daunting. From the beginning of each question to the end, it's important to know what to expect, and to be aware of the areas you might be asked about.

技术面试可能会非常艰巨。 从每个问题的开始到结束,重要的是要知道期望什么,并要知道可能会问到的领域。

Fortunately, there's a way to prepare for any question that may come your way. It involves four parts:

幸运的是,有一种方法可以解决您可能遇到的任何问题。 它包括四个部分:

  1. Understand the question

    了解问题

  2. Discuss tradeoffs of solutions

    讨论解决方案的权衡

  3. Write the code

    编写代码

  4. Test the code

    测试代码

Let's try this technique with sample problem involving LinkedLists.

让我们尝试这种技术,解决涉及LinkedLists的示例问题。

问题 (The Problem)

Question: Given two singly LinkedListNodes, determine if the two lists intersect. Return the intersecting node. Note that the intersection is defined based on reference, not value. If the kth node of the first linked list is the exact same node (by reference) as the jth node of the second linked list, they are intersecting.

问题:给定两个单独的LinkedListNodes,请确定两个列表是否相交。 返回相交的节点。 请注意,交点是根据参考而不是值定义的。 如果第一个链表的第k个节点与第二个链表的第j个节点完全相同(通过引用),则它们是相交的。

步骤1:了解问题。 (Step 1: Understand the question.)

It's really important to know exactly what this question is asking. Some questions we could ask the interviewer are:

确切知道这个问题在问什么是非常重要的。 我们可能会问面试官的一些问题是:

  1. What exactly do we want to return? (A: The intersecting node).

    我们到底要返回什么? (A:相交的节点)。

  2. Does that mean we can assume the linked lists always intersect? (A: Yes)

    这是否意味着我们可以假定链表始终相交? (A:是的)

It's always important to gain a sense of the question before thinking about the approach to solving.

在考虑解决方法之前,先要有一个问题的意识总是很重要的。

步骤2:讨论不同解决方案的权衡。 (Step 2: Discuss the tradeoffs of different solutions.)

One immediate solution is to traverse both linked lists at the same time until you reach an intersection. For this example, we would make a pointer at nodes 2 and 7, and traverse each of them one-by-one until we reach a common node.

一种直接的解决方案是同时遍历两个链表,直到到达相交处。 对于此示例,我们将在节点2和7上创建一个指针 ,并逐个遍历它们,直到到达一个公共节点。

However, as you may have noticed, this will not work as the lengths of the two LinkedLists may differ. What we want to do essentially is “chop off” the beginning part of the longer LinkedListNode, and then iterate repeatedly.

但是,您可能已经注意到,这将不起作用,因为两个LinkedList的长度可能不同。 我们实质上要做的是“切掉”较长的LinkedListNode的开始部分,然后重复进行迭代。

This would be the kind of conversation to have with your interviewer.

这将是与面试官进行的对话。

步骤3:编写代码。 (Step 3: Write the code.)

Below is the method to achieve this.

下面是实现此目的的方法。

We make use of helper methods here. We use getKthNode() to get the kth node of the given linked list. This is helpful when traversing the longer linked list to “chop off” extra nodes.

我们在这里使用辅助方法 。 我们使用getKthNode()获取给定链表的第k个节点。 当遍历较长的链表以“斩断”多余的节点时,这很有用。

We also use getTailAndSize() which captures both the length and the last node of the given list. This is helpful because we definitely need the size to compare lengths of the lists. We also need the tails because if the tails of the two lists are unequal, then they don’t intersect at all.

我们还使用getTailAndSize()来捕获长度和给定列表的最后一个节点。 这很有用,因为我们绝对需要大小来比较列表的长度。 我们还需要尾巴,因为如果两个列表的尾巴不相等,那么它们根本不会相交。

Note that when we say “unequal”, we mean that the two nodes do not reference the same object. Even though they may have the same value and look identical, they must reference the same LinkedListNode to count as equal. (You can find more information on this here.)

请注意,当我们说“不相等”时,是指两个节点没有引用相同的对象 。 即使它们可能具有相同的值并且看起来相同,它们也必须引用相同的LinkedListNode进行计数。 (您可以在这里找到更多信息。)

Going back to the question, if we come across the case where the tails are unequal, we return a failed value (null).

回到问题,如果遇到尾巴不相等的情况,我们将返回失败值(空)。

步骤4:测试代码。 (Step 4: Test the code.)

Below are some good test cases we can add. A helpful rule of thumb for test cases is the following:

以下是一些我们可以添加的良好测试用例。 测试用例的有用经验法则如下:

  • Empty/null case

    空/空箱
  • Considering options in the middle/beginning/end

    考虑中间/开头/结尾的选项
  • Sizes equal or different

    大小相等或不同

This strategy doesn't only apply to LinkedList questions – this would work for arrays, Strings, and essentially any other data structure.

这种策略不仅适用于LinkedList问题,而且适用于数组,字符串以及其他任何数据结构。

For this question, our LinkedList tests would be the following:

对于这个问题,我们的LinkedList测试如下:

  • Two linked lists which intersect at the beginning/middle/end

    在开头/中间/结尾相交的两个链表
  • Both/one linked list is null (should return null)

    两者/一个链接列表为空(应该返回空)
  • Linked lists are the same/different size

    链接列表的大小相同/不同

We’re done!

大功告成!

More Questions:

更多问题:

Interested in breaking into Computer Science? Eager to expand your knowledge base and learn new things? Enjoy problem solving?

有兴趣进入计算机科学领域吗? 渴望扩展您的知识库并学习新事物吗? 喜欢解决问题吗?

If so, SWEPrep may be the newsletter for you. Subscribe to get fully explained interview prompts commonly given in engineering interviews, from Arrays to Dynamic Programming. Questions come out weekly and are also categorized by subject and difficulty. The above post is a Guest Post from the author, Sameer Khoja.

如果是这样, SWEPrep可能是您的新闻通讯。 订阅以获得从数组到动态编程的工程访谈中通常给出的全面解释的访谈提示。 每周都会提出问题,并按照主题和难度进行分类。 上面的帖子是作者Sameer Khoja的来宾帖子。

Subscribe to get full access to the newsletter. Never miss an update.

订阅以获得对新闻通讯的完全访问权限。 不要错过任何更新。

翻译自: https://www.freecodecamp.org/news/how-to-answer-any-technical-interview-question-with-example/

前端面试的作品示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值