字节跳动面试算法面试_如何在不惊慌的情况下进行任何算法面试

字节跳动面试算法面试

by Sun-Li Beatteay

通过孙丽·贝蒂

如何在不惊慌的情况下进行任何编码面试 (How To Approach Any Coding Interview Without Panicking)

Let’s be honest, algorithm problems are still very much a part of the job search. While there’s an ever-expanding list of companies that don’t make you jump through coding hoops, the average developer will encounter a live algorithm challenge sometime in their job hunt. Especially if you want to work for a Big Four or an established startup. So through the hoops we jump.

老实说,算法问题仍然是求职中的重要部分。 虽然有越来越多的公司列表无法使您无所不用其极,但普通开发人员在寻找工作的某个时间会遇到实时算法挑战。 特别是如果您想为四大巨头或知名创业公司工作。 因此,通过篮球,我们跳了起来。

I don’t need to talk about how stressful technical interviews can be. I’m sure most of us know the frustration of walking out of an interview we just bombed and cycling through all the ways we could’ve turned it around. It’s not a fun feeling.

我不需要谈论技术面试的压力。 我敢肯定,我们大多数人都知道走出我们刚刚轰炸的采访并骑自行车进行所有可能解决之道的挫败感。 这不是一种有趣的感觉。

That’s why I’m writing this. For those of you who do end up in an algorithm challenge, how you approach it can make all the difference. Are you the type of person who dives in head-first and figures it out along the way? Or do you have a process you follow to break down the problem into manageable pieces? While the former method may work for some, I exercise the latter.

这就是为什么我要写这个。 对于确实遇到算法挑战的那些人,您如何解决它可能会有所作为。 您是那种先入为主然后沿途弄清楚的人吗? 还是您有遵循的流程将问题分解为可管理的部分? 尽管前一种方法可能对某些人有用,但我会练习后者。

For me, having a set of steps to use to break a problem down is crucial. While it doesn’t guarantee me a solution or job offer, it allows me to manage my stress response. Keeping my panic at a tolerable level helps me to focus. After all, technical interviews should be about demonstrating your ability to problem solve — not your ability to handle multiple people silently judging you without passing out.

对我而言,制定一套解决问题的步骤至关重要。 虽然它不能保证为我提供解决方案或工作机会,但可以管理压力React。 将恐慌症保持在可以容忍的水平可以帮助我集中精力。 毕竟,技术面试应该证明您有解决问题的能力,而不是您能够与多人打交道,而他们不会默默地判断您。

In this article, I want to show you the process I have honed through several technical screens and dozens of mock interviews. It is heavily influenced by the Launch School’s PEDAC system. I use it every single time and it has served me well.

在本文中,我想通过几个技术屏幕和数十次模拟采访向您展示我磨练的过程。 它受到发射学校的PEDAC系统的严重影响。 我每次都使用它,它为我服务很好。

“Fall in love with the process and the results will come.” — Eric Thomas
“爱上了这个过程,结果就会来。” —埃里克·托马斯(Eric Thomas)

The best way to show my process is to demonstrate it in action. So let’s work through a problem together. And to make this as authentic as possible, I’ll pick a problem I’ve never solved before. Though you’re going to have to take my word for it.

展示我的过程的最好方法是在实践中演示它。 因此,让我们一起解决一个问题。 为了使它尽可能真实,我将选择一个我从未解决过的问题。 尽管您将不得不相信我的话。

According to Leetcode, the String to Integer algorithm is a popular interview question. It also has the lowest completion rate of any Medium ranking problem. This should be a good challenge.

根据Leetcode所说 ,“ 字符串到整数”算法是一个受欢迎的面试问题。 它也是所有中等排名问题中最低的完成率。 这应该是一个很好的挑战。

I also picked this problem as it is somewhat practical. This is an actual algorithm that has been implemented in most programming languages. Unlike many other interview challenges (looking at you Coin Change), engineers have actually used this algorithm in real life.

我也选择了这个问题,因为它有些实际。 这是已在大多数编程语言中实现的实际算法。 与许多其他面试挑战(看着您的硬币找零 )不同,工程师实际上在现实生活中使用了此算法。

With that said, let’s dive in. Feel free to follow along in whatever language you want. I will use JavaScript. You can try out my approach or use your own. See if you can even solve it before I do at the end of this post. You might end up one step closer to creating our own language.

话虽如此,让我们潜入。随意使用您想要的任何语言。 我将使用JavaScript。 您可以尝试我的方法或使用自己的方法。 在我结束本文之前,请先看看您是否可以解决它。 您可能会比创建我们自己的语言更近一步。

第1步:用自己的语言重新表达问题 (Step 1: Rephrase the problem in your own words)

For me, this is the most important step. This is my chance to ask my interviewer questions to clarify the requirements and parse out all the crucial information. Furthermore, rewriting the problem into my words gives me the chance to form a mental model and digest the problem.

对我来说,这是最重要的一步。 这是我向面试官提出问题以澄清要求并解析所有关键信息的机会。 此外,将问题改写成我的文字可以使我有机会形成心理模型并消化问题。

For this problem, one question I would ask is whether I’m allowed to use type casting. While the description doesn’t specify it, I will only use JavaScript’s native type casting to convert one character at a time. That’s the sort of restriction I would expect to find in an actual interview.

对于这个问题,我要问的一个问题是是否允许使用类型转换。 尽管描述中未指定,但我只会使用JavaScript的本机类型转换来一次转换一个字符。 我希望在实际采访中会发现这种限制。

After reading the description, these are the key details I came up with.

阅读说明后,这些是我想出的关键细节。

// Given a string, return its appropriate number value.
// Ignore all white-space at the beginning of the string.
// Number may begin with a negative or positive.
// All characters that come after the number should be ignored.
// String is invalid if a character that is not a white-space or sign comes before the number.
// If string does not contain any integer values, it is invalid.
// The return value for any invalid string is 0.
// Resulting integer cannot be larger than (2^31) — 1 or smaller than -(2^31).

Just from these requirements, I’m already starting to envision how I will create this algorithm. It will probably require some looping and quite a bit of conditional logic.

仅从这些要求出发,我已经开始设想如何创建此算法。 它可能需要循环和相当多的条件逻辑。

Some people would probably start coding after this step. For me, it’s still a bit too early to formulate any concrete plans — but my gears are turning.

有些人可能会在此步骤之后开始编码。 对我来说,制定任何具体计划还为时过早-但我的行动正在转向。

步骤2:输入和输出类型 (Step 2: Input and output types)

Many people will see this as a pointless step, but I always make sure to get the inputs and outputs of the algorithm. Either as a code comment or in the corner of the whiteboard.

很多人会认为这是毫无意义的一步,但是我始终确保获得算法的输入和输出。 可以作为代码注释,也可以位于白板的一角。

It serves two functions. First, it solidifies what the parameters of my function will be and what the signature will look like. Leetcode already created the function signature for me but this won’t be the case in an actual interview.

它具有两个功能。 首先,它巩固了我函数的参数以及签名的外观。 Leetcode已经为我创建了函数签名,但是在实际的采访中情况并非如此。

Second, I keep a reminder of the types I’ll be working with. It’s not unheard of for a candidate to fail all the test cases because they forgot to return a string and not an array. I may or may not be speaking from experience…

其次,我提醒我将要使用的类型。 候选程序无法通过所有测试用例并不是闻所未闻的,因为他们忘记了返回字符串而不是返回数组。 我可能会或可能不会从经验中发言...

For our problem, the inputs and outputs are nicely defined in the title.

对于我们的问题,标题中很好地定义了输入和输出。

Input: stringOutput: 32-bit signed integerSignature: myAtoi(str)

步骤3:示例和边缘案例 (Step 3: Examples and Edge Cases)

Now that I’m confident of the inputs and outputs, I want to come up with some test cases. These examples need to cover all the edge cases I can think of. I can only imagine the number of times a candidate has created a working solution, only to have the interviewer come up with an edge case they missed — causing their solution to fall apart.

现在我对输入和输出充满信心,我想提出一些测试用例。 这些示例需要涵盖我能想到的所有极端情况。 我只能想象候选人创建工作解决方案的次数,只是让面试官提出他们错过的一个极端案例-导致他们的解决方案瓦解。

It’s possible that your interviewer will provide some, but I would come up with even more — especially if they aren’t exhaustive. For example, Leetcode has given me some decent test cases.

您的面试官可能会提供一些信息,但我会提出更多的建议-特别是在他们还不详尽的情况下。 例如,Leetcode给了我一些不错的测试用例。

In: “4193 with words”Out: 4193
In: “words and 987”Out: 0
In: “-91283472332”Out: -2147483648

However, these examples are missing some possibilities. What if the number starts with a +? Or what if multiple signs come before a number, such as -+-50?

但是,这些示例缺少一些可能性。 如果数字以+开头怎么办? 或者,如果数字前面有多个符号,例如-+-50怎么办?

Let’s make some better ones.

让我们做一些更好的。

Input: “+50.890”Output: 50
Input: “ -+100”Output: 0
Input: “ !another invalid -10”Output: 0

步骤4:数据结构 (Step 4: Data Structure(s))

Most, if not all, algorithm code challenges involve using a structure to keep track of your data. It’s important to consider which data structure(s) you will use as it will impact your implementation.

大多数(即使不是全部)算法代码挑战都涉及使用一种结构来跟踪数据。 重要的是要考虑将使用哪种数据结构,因为这会影响您的实施。

I know from the problem description that I will be dealing with strings and integers. But will I use another data structure to help convert from one to the other?

从问题描述中我知道我将处理字符串和整数。 但是,我会使用另一种数据结构来帮助从一种转换为另一种吗?

One issue I can already foresee is keeping track of the places of each digit (tens, hundreds, thousands, etc). Since I will not know the length of my integer beforehand, I will use an array to keep track of the integer characters. The array will serve as the interim placeholder for each character before they are converted into the final integer.

我已经可以预见的一个问题是跟踪每个数字的位置(数十个,数百个,数千个等)。 由于我不会事先知道整数的长度,因此我将使用数组来跟踪整数字符。 在将每个字符转换为最终整数之前,该数组将用作每个字符的临时占位符。

While there is most likely a more space efficient solution, I can optimize my solution later. Right now, I just want to go with what makes the most sense for me. It’s better to get a working naive solution than to shoot for the moon and not finish anything.

虽然最有可能提供一种更节省空间的解决方案,但我可以稍后对其进行优化。 现在,我只想选择对我来说最有意义的东西。 获得可行的天真解决方案比为月球射击而不完成任何事情要好。

步骤5:伪代码 (Step 5: Pseudocode)

My penultimate step is to spend some time laying out my algorithm in pseudocode. Interviewers want to see how you think and approach problems. Pseudocode is perfect for that.

我倒数第二个步骤是花一些时间用伪代码布置我的算法。 面试官想看看您如何思考和解决问题。 伪代码是完美的。

An added benefit is that the interviewer will know how to assist you ahead of time. There have been times where I’ve gotten stuck on a problem only to have my interviewer provide subtle hints to keep me going. If you jump into coding without a plan, you could end up confusing both yourself and your interviewer. Do each of you a favor and create an action plan.

另一个好处是,面试官会提前知道如何为您提供帮助。 有时候,我陷入困境只是为了让面试官提供一些微妙的提示,以使我继续前进。 如果您在没有计划的情况下进行编码,那么最终可能会混淆您自己和面试官。 帮忙每个人,并制定一项行动计划。

This is what I came up with.

这就是我想出的。

// Start with index = 0
// While character at current index is white-space  // increment index
// Check if next character is invalid  // return 0
// Check if next character is positive or negative sign  // If negative sign, mark number as negative  // increment index
// Loop through characters starting at current index  // If current character is integer    // Unshift into front of array    // Increment index  // Else, break out of loop
// Loop through string integer array   // Cast string character into integer  // Multiply integer by (10^index) and add to return value
// If string contained negative sign, multiply result value by -1// If result value is less than min, reassign to min// If result value is greater than max, reassign to max
// return value

It may seem like I came up with this out of nowhere, but there was a lot of deliberation and trial-and-error behind the scenes. This is the most time-consuming step because this is where the algorithm is created.

似乎我是无意中想到了这一点,但是幕后却有很多审议和反复试验。 这是最耗时的步骤,因为这是创建算法的地方。

Read over the requirements, inputs/outputs, and edge cases. Ask questions, clarify concepts, and isolate areas of uncertainty to focus on. Find the simplest solution you can think of and work from there.

阅读需求,输入/输出和边缘案例。 提出问题,弄清概念并隔离不确定的区域以进​​行关注。 找到您可以想到的最简单的解决方案,然后从那里开始工作。

Will you need a depth-first search? Sliding window? Divide and conquer? Something else?

您需要深度优先搜索吗? 滑动窗口? 分而治之? 还有吗

If this is the step you struggle with the most, don’t worry. It will get easier with practice. And practice you should. A thorough algorithm design in pseudocode will make the next step fast and easy.

如果这是您最苦恼的一步,请不要担心。 通过实践,它将变得更加容易。 练习吧。 伪代码中详尽的算法设计将使下一步变得快速而轻松。

步骤6:编码! (Step 6: Code!)

Finally!” You’re probably thinking. “That took forever!

最后! ”您可能在想。 “ 那花了永远!

Indeed, I spend a lot of time in planning mood. If an interviewer gives me 45 minutes to finish, I will spend 15–30 minutes thinking and mentally digesting.

确实,我花了很多时间来计划情绪。 如果面试官给我45分钟的时间来完成,我将花15–30分钟思考和思考。

“Give me six hours to chop down a tree and I will spend the first four sharpening the axe.” — Abraham Lincoln
“给我六个小时砍下一棵树,我将用头四个时间来削斧头。” - 亚伯拉罕·林肯

In fact, coding is the least important step for me. All the heavy lifting has already been done. Now I just need to interpret my mental model into code.

实际上,编码对我来说是最不重要的一步。 所有繁重的工作已经完成。 现在,我只需要将我的思维模型解释为代码。

Additionally, how I code this solution in an interview setting won’t be the same as how I code it in real life. Heck, a real interview solution would look different than the answer I came up with for this article. Several factors affect how I code in an interview, such as time and responsiveness of the interviewer.

此外,我在面试环境中编码此解决方案的方式与在现实生活中编码方式不同。 哎呀,一个真正的面试解决方案看起来与我为本文得出的答案有所不同。 有几个因素会影响我在面试中的编码方式,例如面试官的时间和响应能力。

Without access to Google or sufficient time to refactor, I just want to write something that works. And there’s no guarantee I would even achieve that.

在无法访问Google或没有足够的时间进行重构的情况下,我只想编写可行的方法。 而且无法保证我会实现这一目标。

But that’s not the point of this post. Yes, it’s possible I wouldn’t have solved this question in an interview. But up until this point, I have de-structured the challenge into its key components. I know I can solve it and I have put myself in the best position to do so. A good interviewer will see that.

但这不是本文的重点。 是的,我可能不会在面试中解决这个问题。 但是到现在为止,我已经将挑战分解为关键要素。 我知道我可以解决这个问题,而且我已将自己置于最佳位置。 一个好的面试官会看到的。

In a technical screen or onsite, it’s not about the code. It’s how you come up with it.

在技​​术屏幕或现场,与代码无关。 这就是您的想法。

If you are interested in comparing solutions, here’s the one I came up with:

如果您有兴趣比较解决方案,以下是我提出的解决方案:

This solution is not the most efficient. According to Leetcode, it only beats 25% of the other submissions.

此解决方案不是最有效的。 根据Leetcode的说法,它仅击败了其他提交作品的25%。

However, it would pass most technical interviews. An interviewer might ask me to optimize it for space or time, but those are things that can be included on further iterations if time permits. You don’t need to come up with them on the first try.

但是,它将通过大多数技术面试。 面试官可能会要求我针对空间或时间对其进行优化,但是如果时间允许,这些可以包含在进一步的迭代中。 您无需在第一次尝试时就提出建议。

The point of using a process is to have a systemic approach to break down any challenge. It works whether you use in your job on a daily basis or in a technical interview. By using it in an interview, you can keep your panic at bay by focusing on the challenge and not your emotions.

使用流程的目的是要有系统的方法来应对所有挑战。 无论您是在日常工作中还是在技术面试中使用它,它都有效。 通过在采访中使用它,您可以专注于挑战而不是情绪,从而避免恐慌。

If you don’t have a process, start making one. You can use PEDAC or develop your own. Just make sure it helps you create solutions in which you’re confident.

如果您没有流程,请开始制作。 您可以使用PEDAC或自行开发。 只要确保它可以帮助您创建自信的解决方案即可。

For example, you may have noticed the use of constants, helper functions, and regex in my solution. Those are all tricks I’ve picked up that help me isolate complexity in an interview. The more my code reads like English, the less confused I get when writing it, and the faster I work. It may be a bit verbose, but I like it. Do what works for you.

例如,您可能已经注意到在我的解决方案中使用了常量,辅助函数和正则表达式。 这些都是我掌握的技巧,可帮助我隔离面试中的复杂性。 我的代码读起来像英语一样多,写代码时我就不太困惑,而且我工作得更快。 可能有些冗长,但我喜欢。 做对您有用的事情。

If there’s already a procedure you use, practice and perfect it. Don’t wait until your onsite interview to start fine-tuning. Experiment in mock interviews. Pramp and Interviewing.io are perfect tools for that.

如果您已经使用了某个程序,请练习并完善它。 不要等到您的现场采访开始进行微调。 在模拟访谈中进行实验。 PrampInterviewing.io是实现此目的的完美工具。

Remember, if all else fails, trust the process.

请记住,如果其他所有方法均失败,请信任该过程。

If this article has resonated with you, please leave some claps ? !

如果这篇文章引起您的共鸣,请留下一些鼓掌? !

As always, happy coding!

一如既往,编码愉快!

翻译自: https://www.freecodecamp.org/news/how-to-approach-any-algorithm-interview-without-panicking-b6d7ae5c050/

字节跳动面试算法面试

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值