常用算法面试
Since graduating from coding boot camp, I’ve been focusing most of my time on building apps and websites; which is good for a resume but what about the actual interviewing process? Most developers I’ve spoken with have applauded this approach but have been quick to let me know that those apps won’t matter if I can’t pass algorithm questions that will most definitely be thrown my way. So today I am going to be sharing two common coding algorithms as well as their solutions and some links to resources that will help you expand your knowledge of algorithms.
自从编码新手训练营毕业以来,我一直将大部分时间专注于构建应用程序和网站。 哪个对简历有益,但是实际的面试过程又如何呢? 我与之交谈过的大多数开发人员都对这种方法表示赞赏,但是很快就让我知道,如果我不能通过肯定会引发问题的算法问题,那么这些应用程序将无关紧要。 因此,今天我将分享两种常见的编码算法及其解决方案,以及与资源的一些链接,这些资源将帮助您扩展算法知识。
FizzBuzz is definitely one of the most common algorithms you’ll be asked to solve. The instructions usually ask you to write a program that console logs or returns number 1 to n. If a number is divisible(or a multiple) of 3 - print ‘fizz’, a multiple of 5 - print ‘buzz’, a multiple of 3 and 5 - print ‘fizzbuzz’. For this solution, you’ll need to understand ‘for loops’, ‘if else’ statements, and the modulus(remainder) operator. The first item we should consider is how do we check that a number is divisible of any number. The modulus operator allows us to check what the remainder of a number will be.
FizzBuzz绝对是您需要解决的最常见算法之一。 这些说明通常会要求您编写一个控制台记录日志或将数字1返回到n的程序。 如果数字是3的整数倍(或倍数),则打印“嘶嘶声”;如果是5的整数倍,则打印“嗡嗡声”; 3和5的整数倍,则打印“嘶嘶声”。 对于此解决方案,您需要了解“ for循环”,“ if else ”语句以及模数(余数)运算符。 我们应该考虑的第一项是如何检查数字是否可以被任何数整除。 模运算符使我们可以检查数字的余数。
The result of the function, pictured above, will be 1. This is because 3 times 3 is 9 and the remainder of 9 into 10 is 1. So with that brief explanation, let’s build a statement to check if a number is a multiple of 3 and 5 (which must also be a multiple of 15).
如上图所示,该函数的结果将为1。这是因为3乘3为9,而9乘以10的余数为1。因此,通过简要的说明,让我们构建一个语句来检查数字是否为数字的倍数3和5(也必须是15的倍数)。
Boom bop. Bam boom. bo Bam繁荣。So what we are saying here is, if a number(n) has a remainder of 0 (===0) for both 3 and (&&) 5, then console log ‘fizzbuzz’. Otherwise console log ‘not a multiple of 3 or 5’. We could also write ‘if(n % 15 === 0)’ to shorten the code as 15 is the least common multiple of 3 and 5. Now that we have the hardest part out of the way, let’s finish off the code.
因此,这里我们要说的是,如果数字(n)的3和(&&)5的余数均为0(=== 0),则控制台日志“ fizzbuzz”。 否则,控制台日志“不是3或5的倍数”。 我们还可以编写'if(n%15 === 0)'来缩短代码,因为15是3和5的最小公倍数。现在,我们已经完成了最困难的部分,让我们结束代码。
Wonderful! As you can see on line 16 — our for statement is creating a counter variable ‘i’, setting it to 1 and asking if i is less than or equal to ‘n’ then run the following code and increase ‘i’ by one after each iteration. The code being run is checking to see if the variable ’n’ is divisible by 3 and 5, then 3, then 5. If it’s doesn’t pass any of the statements then simply console log the number. There you go, ‘fizzbuzz’ is passed so let’s try one more.
精彩! 如您在第16行所见,我们的for语句正在创建一个计数器变量'i',将其设置为1并询问i是否小于或等于'n',然后运行以下代码并将“ i”加一每次迭代。 正在运行的代码正在检查变量'n'是否可被3和5,3,5整除。如果未传递任何语句,则只需控制台记录数字即可。 到这里,“ fizzbuzz”通过了,让我们再尝试一次。
A palindrome is a word or series of words that are the same whether they are spelt forwards or backwards, such as ‘racecar’. This one may seem daunting but it’s fairly easy once you see the solution. With this function we’ll be using some JS string and array methods, .split, .reverse, and .join. What we want to do is first split the string into an array using .split(‘’). This will split each character of a string and place it into an array, like the example below.
回文是指一个单词或一系列单词,无论是向前拼写还是向后拼写都相同,例如“ racecar”。 这似乎令人生畏,但是一旦您看到解决方案,它就相当容易。 通过此功能,我们将使用一些JS字符串和数组方法。 分裂。 反向和.join 。 我们要做的是首先使用.split('')将字符串拆分为一个数组。 它将拆分字符串的每个字符并将其放入数组,如以下示例所示。
Side note: If we were to put a space in the quotation, then it would split the string by each space console logging [‘example’]. We don’t want this. 旁注:如果我们在引号中放置一个空格,则它将通过每个空格控制台日志记录['example']拆分字符串。 我们不想要这个。Now we want to use the .reverse method on our array, which as the name implies, reverse the array.
现在,我们要在数组上使用.reverse方法,顾名思义,该方法将数组反转。
tsomla ereht(almost there) tsomla ereht(几乎在那里)Now we want to .join() each element of the array and check if it is the same as the string passed into the function.
现在我们要.join()数组的每个元素,并检查它是否与传递给函数的字符串相同。
Be careful using == and ===. The latter will return a boolean based on if the items being compared are EXACTLY the same. In this case they are not. 使用==和===时要小心。 后者将根据所比较的项目是否完全相同返回布尔值。 在这种情况下,它们不是。 Success! <- almost a palindrome :) 成功! <-几乎是回文:)As you can see, that wasn’t too bad. It does take time to learn your string and array methods but you’ll find yourself using them the more you practice your algorithms. Now before I share links to some amazing sources, please understand that this will take time and patience. For some of us, it is a challenge to break problems down and be methodical about every step. For others, they will blink and have the answer. Don’t worry too much about where you fall, just about how you get back up and try again. If you find yourself getting frustrated, then take a 10–15 minute break doing something else. If you can’t figure out a code then don’t feel ashamed to research the answer online or in a book. We all do it. The only way to get better at understanding code and algorithms is through trial, error, learning from others and practice.
如您所见,这还不错。 学习字符串和数组方法确实需要时间,但是实践算法越多,就会发现自己正在使用它们。 现在,在我分享一些惊人信息的链接之前,请理解,这需要时间和耐心。 对于我们中的某些人而言,将问题分解为每个步骤并保持系统性是一个挑战。 对于其他人,他们会眨眼并找到答案。 不必担心坠落的地方,只需担心如何站起来再试一次。 如果您发现自己感到沮丧,请休息10-15分钟,做其他事情。 如果您找不到代码,那么不要在网上或书中研究答案就感到羞耻。 我们都做。 更好地理解代码和算法的唯一方法是通过尝试,错误,向他人学习和实践。
With all that being said, here are some great ways to learn and test your algorithm skills as well as javascript methods:
综上所述,这里有一些学习和测试算法技能以及javascript方法的好方法:
Cracking The Coding Interview by Gayle Laakmann McDowell
Gayle Laakmann McDowell撰写的采访访谈
Codewars.com
Codewars.com
Algorithms and Data Structures by Stephen Grider
Stephen Grider的算法和数据结构
W3 Schools
W3学校
翻译自: https://medium.com/@timothydan/coding-interviews-common-algorithms-8ecf8787731
常用算法面试