50+ 精选数据结构和算法面试问题

https://dev.to/javinpaul/50-data-structure-and-algorithms-problems-from-coding-interviews-4lh2

There are a lot of computer science graduates and programmers applying for programming, coding, and software development roles at startups like Uber and Netflix; big organizations like Amazon, Microsoft, and Google; and service-based companies like Infosys or Luxsoft, but many of them have no idea of what kind of programming interview questions to expect when you’re applying for a job with these companies.

In this article, I’ll share some frequently *asked programming interview questions*from different interviews for programmers at different levels of experience,from people who have just graduated from college to programmers with one to two years of experience.

Coding interviews are comprised mainly of data structure and algorithm-based questions as well as some of the logical questions such as, How do you swap two integers without using a temporary variable?

I think it’s helpful to divide coding interview questions into different topic areas. The topic areas I’ve seen most often in interviews are array, linked list, string, binary tree, as well as questions from algorithms (e.g. string algorithm, sorting algorithms like quicksort or radix sort, and other miscellaneous ones),and that’s what you will find in this article.

It’s not guaranteed that you will be asked these coding or data structure and algorithmic questions, but they will give you enough of an idea of the kinds of questions you can expect in a real programming job interview.

Once you have gone through these questions, you should feel confident enough to attend any telephonic or face-to-face interviews.

Btw, there is no point in attempting these questions if you don’t have sufficient knowledge of essential Data Structure and Algorithms or you have not touched them from ages.

In that case, you should take a good course like Algorithms and Data Structures Part 1 and 2 By Robert Horvickto refresh your DS and algorithms skills.

Top 50 Algorithms and Coding Interview Questions
Without any further ado, here is my list of some of the most frequently asked coding interview questions from programming job interviews:

  1. Array Coding Interview Questions
    An array is the most fundamental data structure, which stores elements at contiguous memory location. It is also one of the darling topics of interviewers and you will hear a lot of questions about an array in any coding interview, e.g. reversing an array, sorting the array, or searching elements on the array.

The key benefit of an array data structure is that it offers fast O(1) search if you know the index, but adding and removing an element from an array is slow because you cannot change the size of the array once it’s created.

In order to create a shorter or longer array, you need to create a new array and copy all elements from old to new.

The key to solving array-based questions is having a good knowledge of array data structure as well as basic programming constructors such as loop, recursion, and fundamental operators.

Here are some of the popular array-based coding interview questions for your practice:

How do you find the missing number in a given integer array of 1 to 100? (solution)
How do you find the duplicate number on a given integer array?(solution)
How do you find the largest and smallest number in an unsorted integer array? (solution)
How do you find all pairs of an integer array whose sum is equal to a given number?(solution)
How do you find duplicate numbers in an array if it contains multiple duplicates?(solution)
How are duplicates removed from a given array in Java? (solution)
How is an integer array sorted in place using the quicksort algorithm? (solution)
How do you remove duplicates from an array in place? (solution)
How do you reverse an array in place in Java? (solution)
How are duplicates removed from an array without using any library? (solution)
These questions will not only help you to develop your problem-solving skills but also improve your knowledge of the array data structure.

If you need more advanced questions based upon array then you can see also seeThe Coding Interview Bootcamp: Algorithms + Data Structures, a bootcamp style course on algorithms, especially designed for interview preparation to get a job on technical giants like Google, Microsoft, Apple, Facebook etc.

And, if you feel 10 is not enough questions and you need more practice, then you can also check out this list of 30 array questions.

  1. Linked List Programming Interview Questions
    A linked list is another common data structure that complements the array data structure. Similar to the array, it is also a linear data structure and stores elements in a linear fashion.

However, unlike the array, it doesn’t store them in contiguous locations;instead, they are scattered everywhere in memory, which is connected to each other using nodes.

A linked list is nothing but a list of nodes where each node contains the value stored and the address of the next node.

Because of this structure, it’s easy to add and remove elements in a linked list, as you just need to change the link instead of creating the array, but the search is difficult and often requires O(n) time to find an element in the singly linked list.

This article provides more information on the difference between an array and linked list data structures.

It also comes in varieties like a singly linked list, which allows you to traverse in one direction (forward or reverse); a doubly linked list, which allows you to traverse in both directions (forward and backward); and finally, the circular linked list, which forms a circle.

In order to solve linked list-based questions, a good knowledge of recursion is important, because a linked list is a recursive data structure.

If you take one node from a linked list, the remaining data structure is still a linked list, and because of that, many linked list problems have simpler recursive solutions than iterative ones.

Here are some of the most common and popular linked list interview questions and their solutions:

How do you find the middle element of a singly linked list in one pass? (solution)
How do you check if a given linked list contains a cycle? How do you find the starting node of the cycle? (solution)
How do you reverse a linked list? (solution)
How do you reverse a singly linked list without recursion? (solution)
How are duplicate nodes removed in an unsorted linked list? (solution)
How do you find the length of a singly linked list? (solution)
How do you find the third node from the end in a singly linked list? (solution)
How do you find the sum of two linked lists using Stack? (solution)
These questions will help you to develop your problem-solving skills as well as improve your knowledge of the linked list data structure.

If you are having trouble solving these linked list coding questions then I suggest you refresh your data structure and algorithms skill by going through Data Structures and Algorithms: Deep Dive** Using Java** course.

You can also check out this list of30 linked list interview questions for more practice questions.

  1. String Coding Interview Questions
    Along with array and linked list data structures, a string is another popular topic on programming job interviews. I have never participated in a coding interview where no string-based questions were asked.

A good thing about the string is that if you know the array, you can solve string-based questions easily because strings are nothing but a character array.

So all the techniques you learn by solving array-based coding questions can be used to solve string programming questions as well.

Here is my list of frequently asked string coding questions from programming job interviews:

How do you print duplicate characters from a string? (solution)
How do you check if two strings are anagrams of each other? (solution)
How do you print the first non-repeated character from a string? (solution)
How can a given string be reversed using recursion? (solution)
How do you check if a string contains only digits? (solution)
How are duplicate characters found in a string? (solution)
How do you count a number of vowels and consonants in a given string? (solution)
How do you count the occurrence of a given character in a string? (solution)
How do you find all permutations of a string? (solution)
How do you reverse words in a given sentence without using any library method? (solution)
How do you check if two strings are a rotation of each other? (solution)
How do you check if a given string is a palindrome? (solution)
These questions help improve your knowledge of string as a data structure. If you can solve all these String questions without any help then you are in good shape.

For more advanced questions, I suggest you solve problems given in theAlgorithm Design Manual by Steven Skiena, a book with the toughest algorithm questions.

If you need more practice, here is another list of 20 string coding questions.

  1. Binary Tree Coding Interview Questions
    So far, we have looked at only the linear data structure, but all information in the real world cannot be represented in linear fashion, and that’s where tree data structure helps.

Tree data structure is a data structure that allows you to store your data in a hierarchical fashion. Depending on how you store data, there are different types of trees, such as a binary tree, where each node has, at most, two child nodes.a

Along with its close cousin binary search tree, it’s also one of the most popular tree data structures. Therefore, you will find a lot of questions based on them, such as how to traverse them, count nodes, find depth, and check if they are balanced or not.

A key point to solving binary tree questions is a strong knowledge of theory, e.g. what is the size or depth of the binary tree, what is a leaf, and what is a node, as well as an understanding of the popular traversing algorithms, e.g. pre-, post-, and in-order traversal.

Here is a list of popular binary tree-based coding questions from software engineer or developer job interviews:

How is a binary search tree implemented? (solution)
How do you perform preorder traversal in a given binary tree?(solution)
How do you traverse a given binary tree in preorder without recursion?(solution)
How do you perform an inorder traversal in a given binary tree?*(solution)
How do you print all nodes of a given binary tree using inorder traversal without recursion? (solution)
How do you implement a postorder traversal algorithm? (solution)
How do you traverse a binary tree in postorder traversal without recursion?(solution)
How are all leaves of a binary search tree printed?(solution)
How do you count a number of leaf nodes in a given binary tree?(solution)
How do you perform a binary search in a given array?(solution)
If you feel that your understanding of binary tree coding is inadequate and you can’t solve these questions on your own, I suggest you go back and pick a good data structure and algorithm course like From 0 to 1: Data Structures & Algorithms in Java.

If you need some more recommendations, here is my list of useful data structure algorithm books and courses to start with.

  1. Miscellaneous Coding Interview Questions
    Apart from data structure-based questions, most of the programming job interviews also ask algorithm, design, bit manipulation, and general logic-based questions, which I’ll describe in this section.

It’s important that you practice these concepts because sometimes they become tricky to solve in the actual interview. Having practiced them before not only makes you familiar with them but also gives you more confidence in explaining the solution to the interviewer.

How is a bubble sort algorithm implemented? (solution)
How is an iterative quicksort algorithm implemented? (solution)
How do you implement an insertion sort algorithm? (solution)
How is a merge sort algorithm implemented? (solution)
How do you implement a bucket sort algorithm?(solution)
How do you implement a counting sort algorithm?(solution)
How is a radix sort algorithm implemented?(solution)
How do you swap two numbers without using the third variable? (solution)
How do you check if two rectangles overlap with each other? (solution)
How do you design a vending machine? (solution)
If you need more such coding questions you can take help from books like Cracking The Code Interview, byGayle Laakmann McDowellwhich presents 189+ Programming questions and solution. A good book to prepare for programming job interviews in a short time.

By the way, the more questions you solve in practice, the better your preparation will be. So, if you think 50 is not enough and you need more, then check out these additional 50 programming questionsfor telephone interviews and these books and courses for a more thorough preparation.

Now You’re Ready for the Coding Interview
These are some of the most common questions outside of data structure and algorithms that help you to do really well in your interview.

I have also shared a lot of these questions on my blog, so if you are really interested, you can always go there and search for them.

These common coding, data structure, and algorithm questions are the ones you need to know to successfully interview with any company, big or small, for any level of programming job.

If you are looking for a programming or software development job in 2018, you can start your preparation with this list of coding questions.

This list provides good topics to prepare and also helps assess your preparation to find out your areas of strength and weakness.

A good knowledge of data structure and algorithms is important for success in coding interviews and that’s where you should focus most of your attention.

Further Learning
Data Structures and Algorithms: Deep Dive Using Java
10 Books to Prepare Technical Programming/Coding Job Interviews
10 Algorithm Books Every Programmer Should Read
Top 5 Data Structure and Algorithm Books for Java Developers
From 0 to 1: Data Structures & Algorithms in Java
Data Structure and Algorithms Analysis — Job Interview

Closing Notes
Thanks, You made it to the end of the article … Good luck with your programming interview! It’s certainly not going to be easy, but by following this roadmap and guide, you are one step closer to becoming a DevOps engineer.

If you like this article, then please share with your friends and colleagues, and don’t forget to follow javinpaul on Twitter!

P.S. — If you need some FREE resources, you can check out this list of free data structure and algorithm courses to start your preparation.

有许多计算机科学专业毕业生和程序员在Uber和Netflix等初创公司申请编程,编码和软件开发职位; 亚马逊,微软和谷歌等大型组织; 像Infosys或Luxsoft这样的服务型公司,但他们中的许多人都不知道当你在这些公司申请工作时会遇到什么样的编程面试问题。

在本文中,我将与不同经验水平的程序员分享一些常见的编程面试问题,从刚从大学毕业的人到具有一到两年经验的程序员。

编码访谈主要包括数据结构和基于算法的问题以及一些逻辑问题,例如,如何在不使用临时变量的情况下交换两个整数?

我认为将编码面试问题划分到不同的主题领域是有帮助的。我在访谈中经常看到的主题领域是数组,链表,字符串,二叉树,以及来自算法的问题(例如字符串算法,排序算法,如quicksort或radix排序,以及其他杂项),这就是什么你会在这篇文章中找到。

我们无法保证会向您询问这些编码或数据结构和算法问题,但它们会让您充分了解在实际编程工作面试中可能遇到的各种问题。

一旦你完成了这些问题,你应该有足够的信心参加任何电话或面对面访谈。

顺便说一下,如果你对基本的数据结构和算法没有足够的知识,或者你没有多年触及它们,那么尝试这些问题毫无意义。

在这种情况下,你应该采用像算法和数据结构第1部分和第2部分这样的好的课程作者Robert Horvick 来刷新你的DS和算法技能。

50个算法和编码面试问题
没有任何进一步的麻烦,这里是我的编程工作面试中一些最常见的编码面试问题列表:

1.数组编码面试问题
数组是最基本的数据结构,它将元素存储在连续的内存位置。它也是采访者的宠儿主题之一,你会在任何编码采访中听到很多关于数组的问题,例如反转数组,排序数组或搜索数组中的元素。

数组数据结构的主要优点是,如果您知道索引,它会提供快速的O(1)搜索,但是从数组中添加和删除元素的速度很慢,因为一旦创建了数组,就无法更改数组的大小。

要创建更短或更长的数组,您需要创建一个新数组并将所有元素从旧复制到新。

解决基于数组的问题的关键是熟悉数组数据结构以及基本编程构造函数,如循环,递归和基本运算符。

以下是一些针对您的练习的基于阵列的流行编码面试问题:

如何在给定的1到100的整数数组中找到缺失的数字?(解决方案)
如何在给定的整数数组上找到重复的数字?(解决方案)
如何在未排序的整数数组中找到最大和最小的数字?(解决方案)
你如何找到所有对的整数数组,其总和等于给定的数字?(解决方案)
如果数组包含多个重复项,如何在数组中找到重复的数字?(解决方案)
如何从Java中的给定数组中删除重复项?(解决方案)
如何使用quicksort算法对整数数组进行排序?(解决方案)
如何从阵列中删除重复项?(解决方案)
你如何在Java中反转数组?(解决方案)
如何在不使用任何库的情况下从数组中删除重复项?(解决方案)
这些问题不仅可以帮助您提高解决问题的能力,还可以提高您对阵列数据结构的了解。

如果您需要基于阵列的更高级的问题,那么您还可以看到编码面试训练营:算法+数据结构,算法的训练营风格课程,专门用于面试准备,以获得谷歌,微软,苹果等技术巨头的工作,Facebook等

而且,如果您觉得10个问题不够而且需要更多练习,那么您还可以查看30个阵列问题列表。

2.链接列表编程面试问题
甲链表是另一个常见的数据结构,它补充了阵列。与数组类似,它也是线性数据结构,并以线性方式存储元素。

但是,与数组不同,它不会将它们存储在连续的位置; 相反,它们分散在内存中的任何地方,它们使用节点相互连接。

链表只不过是一个节点列表,其中每个节点包含存储的值和下一个节点的地址。

由于这种结构,在链表中添加和删除元素很容易,因为您只需要更改链接而不是创建数组,但搜索很困难,并且通常需要花费O(n)时间来查找元素。单链表。

此文章提供了在阵列和链表数据结构之间的区别的详细信息。

它还有各种类似链表,可以让你在一个方向上移动(前进或后退); 一个双向链表,它允许你在两个方向(向前和向后)移动; 最后,圆形链表,形成一个圆圈。

为了解决基于链表的问题,良好的递归知识很重要,因为链表是递归数据结构。

如果从链表中获取一个节点,则剩余的数据结构仍然是链表,因此,许多链表问题具有比迭代解决方案更简单的递归解决方案。

以下是一些最常见和最受欢迎的链接列表面试问题及其解决方案:

如何在一次通过中找到单链表的中间元素?(解决方案)
如何检查给定的链表是否包含循环?你如何找到循环的起始节点?(解决方案)
你如何扭转链表?(解决方案)
如何在没有递归的情况下反转单链表?(解决方案)
如何在未排序的链表中删除重复节点?(解决方案)
你如何找到单链表的长度?(解决方案)
如何在单链表中找到最后的第三个节点?(解决方案)
如何使用Stack找到两个链表的总和?(解决方案)
这些问题将帮助您提高解决问题的能力,并提高您对链表数据结构的了解。

如果您在解决这些链表编码问题时遇到问题,那么我建议您通过数据结构和算法:深入使用Java课程来刷新您的数据结构和算法技能。

您还可以查看30个链接列表面试问题列表,了解更多练习题。

3.字符串编码面试问题
与数组和链表数据结构一起,字符串是编程工作访谈的另一个热门话题。我从未参加过没有问过基于字符串的问题的编码面试。

字符串的一个好处是,如果你知道数组,你可以很容易地解决基于字符串的问题,因为字符串只是一个字符数组。

因此,通过求解基于数组的编码问题所学到的所有技术也可用于解决字符串编程问题。

以下是编程求职面试中常见的字符串编码问题列表:

如何从字符串中打印重复的字符?(解决方案)
你如何检查两个字符串是否是彼此的字谜?(解决方案)
如何从字符串中打印第一个不重复的字符?(解决方案)
如何使用递归来反转给定的字符串?(解决方案)
如何检查字符串是否只包含数字?(解决方案)
如何在字符串中找到重复的字符?(解决方案)
你如何计算给定字符串中的元音和辅音?(解决方案)
如何计算字符串中给定字符的出现次数?(解决方案)
你如何找到字符串的所有排列?(解决方案)
如何在不使用任何库方法的情况下反转给定句子中的单词?(解决方案)
你如何检查两个字符串是否相互旋转?(解决方案)
你如何检查给定的字符串是否是回文?(解决方案)
这些问题有助于提高您对字符串作为数据结构的了解。如果您可以在没有任何帮助的情况下解决所有这些String问题,那么您的状态良好。

对于更高级的问题,我建议你解决Steven Skiena在算法设计手册中给出的问题,这是一本有最棘手的算法问题的书。

如果您需要更多练习,这里是另一个20个字符串编码问题的列表。

4.二叉树编码面试问题
到目前为止,我们只研究了线性数据结构,但现实世界中的所有信息都无法以线性方式表示,而这正是树数据结构所帮助的地方。

树数据结构是一种数据结构,允许您以分层方式存储数据。根据您存储数据的方式,有不同类型的树,例如二叉树,其中每个节点最多有两个子节点.a

除了它的近亲二元搜索树,它也是最流行的树数据结构之一。因此,您会发现很多基于它们的问题,例如如何遍历它们,计算节点,查找深度,以及检查它们是否平衡。

解决二叉树问题的关键是强大的理论知识,例如二叉树的大小或深度,叶子是什么,节点是什么,以及对流行的遍历算法的理解,例如前,后和有序遍历。

以下是软件工程师或开发人员访谈中流行的基于二叉树的编码问题列表:

如何实现二叉搜索树?(解决方案)
如何在给定的二叉树中执行前序遍历?(解决方案)
如何在没有递归的情况下预先遍历给定的二叉树?(解决方案)
如何在给定的二叉树中执行inorder遍历?(解决方案)
如何在没有递归的情况下使用inorder遍历打印给定二叉树的所有节点?(解决方案)
你如何实现后序遍历算法?(解决方案)
如何在没有递归的情况下遍历后序遍历中的二叉树?(解决方案)
如何打印二叉搜索树的所有叶子?(解决方案)
如何计算给定二叉树中的多个叶节点?(解决方案)
如何在给定数组中执行二进制搜索?(解决方案)
如果您觉得您对二叉树编码的理解不充分而且您无法自己解决这些问题,我建议您回过头来选择一个好的数据结构和算法课程,如从0到1:Java中的数据结构和算法。

如果您需要更多建议,请参阅我的有用数据结构算法书籍和课程列表。

5.杂项编码面试问题
除了基于数据结构的问题之外,大多数编程工作访谈还会询问算法,设计,位操作和基于逻辑的一般问题,我将在本节中对其进行描述。

练习这些概念很重要,因为有时在实际面试中解决这些概念很棘手。之前练习它们不仅让你熟悉它们,而且还让你更有信心向面试官解释解决方案。

如何实现冒泡排序算法?(解决方案)
如何实现迭代快速排序算法?(解决方案)
你如何实现插入排序算法?(解决方案)
如何实现合并排序算法?(解决方案)
如何实现桶排序算法?(解决方案)
你如何实现计数排序算法?(解决方案)
如何实现基数排序算法?(解决方案)
如何在不使用第三个变量的情况下交换两个数字?(解决方案)
如何检查两个矩形是否相互重叠?(解决方案)
你如何设计自动售货机?(解决方案)
如果你需要更多这样的编码问题,你可以从像Cracking The Code Interview这样的书籍中获取帮助,由Gayle Laakmann McDowell 提供189 +编程问题和解决方案。一本好书,可以在短时间内编写求职面试。

顺便说一下,你在实践中解决的问题越多,你的准备就越好。因此,如果您认为50还不够而且您需要更多,那么请查看这些额外的50个编程问题, 以便进行电话访谈以及这些书籍和课程,以便进行更全面的准备。

现在,您已准备好进行编码面试
这些是数据结构和算法之外的一些最常见的问题,可以帮助您在面试中做得很好。

我也在我的博客上分享了很多这些问题,所以如果你真的很感兴趣,你可以随时去那里搜索它们。

这些常见的编码,数据结构和算法问题是您在任何级别的编程工作中成功访问任何公司(大或小)时需要了解的问题。

如果您正在寻找2018年的编程或软件开发工作,您可以使用此编码问题列表开始准备。

此列表提供了准备的好主题,也有助于评估您的准备工作,以找出您的优势和劣势领域。

熟悉数据结构和算法对于成功编写访谈非常重要,而且您应该将注意力集中在大部分时间。

结论
谢谢,你完成了文章的最后…祝你的编程面试好运!这当然不容易,但通过遵循这个路线图和指南,您离成为DevOps工程师又近了一步。

原文:

https://hackernoon.com/50-data-structure-and-algorithms-interview-questions-for-programmers-b4b1ac61f5b0?spm=5176.11156381.0.0.14835740h1b2th

你可能感兴趣的:(java)