赞
踩
数据结构面试题编程题
by Fahim ul Haq
通过Fahim ul Haq
Niklaus Wirth, a Swiss computer scientist, wrote a book in 1976 titled Algorithms + Data Structures = Programs.
瑞士计算机科学家Niklaus Wirth在1976年写了一本书,名为《 算法+数据结构=程序》。
40+ years later, that equation still holds true. That’s why software engineering candidates have to demonstrate their understanding of data structures along with their applications.
40多年后,这个等式仍然成立。 这就是为什么软件工程候选人必须证明他们对数据结构及其应用程序的理解。
Almost all problems require the candidate to demonstrate a deep understanding of data structures. It doesn’t matter whether you have just graduated (from a university or coding bootcamp), or you have decades of experience.
几乎所有问题都要求考生表现出对数据结构的深刻理解。 您是否刚刚毕业(从大学或编程训练营毕业),或者您有数十年的经验都没关系。
Sometimes interview questions explicitly mention a data structure, for example, “given a binary tree.” Other times it’s implicit, like “we want to track the number of books associated with each author.”
有时访谈问题明确提到数据结构,例如“给定二叉树”。 其他时候它是隐式的,例如“我们要跟踪与每个作者关联的书籍数量”。
Learning data structures is essential even if you’re just trying to get better at your current job. Let’s start with understanding the basics.
即使您只是想在当前工作中变得更好,学习数据结构也是必不可少的。 让我们从了解基础开始。
Simply put, a data structure is a container that stores data in a specific layout. This “layout” allows a data structure to be efficient in some operations and inefficient in others. Your goal is to understand data structures so that you can pick the data structure that’s most optimal for the problem at hand.
简而言之,数据结构是一个以特定布局存储数据的容器。 这种“布局”使数据结构在某些操作中有效,而在另一些操作中效率低下。 您的目标是了解数据结构,以便选择最适合当前问题的数据结构。
As data structures are used to store data in an organized form, and since data is the most crucial entity in computer science, the true worth of data structures is clear.
由于数据结构用于以有组织的形式存储数据,并且由于数据是计算机科学中最关键的实体,因此数据结构的真正价值显而易见。
No matter what problem are you solving, in one way or another you have to deal with data — whether it’s an employee’s salary, stock prices, a grocery list, or even a simple telephone directory.
无论您要解决什么问题,都必须以一种或另一种方式处理数据-无论是员工的薪水,股票价格,购物清单还是简单的电话簿。
Based on different scenarios, data needs to be stored in a specific format. We have a handful of data structures that cover our need to store data in different formats.
根据不同的场景,数据需要以特定的格式存储。 我们有一些数据结构可以满足我们以不同格式存储数据的需求。
Let’s first list the most commonly used data structures, and then we’ll cover them one by one:
让我们首先列出最常用的数据结构,然后逐一介绍它们:
An array is the simplest and most widely used data structure. Other data structures like stacks and queues are derived from arrays.
数组是最简单,使用最广泛的数据结构。 其他数据结构(如堆栈和队列)是从数组派生的。
Here’s an image of a simple array of size 4, containing elements (1, 2, 3 and 4).
这是大小为4的简单数组的图像,其中包含元素(1、2、3和4)。
Each data element is assigned a positive numerical value called the Index, which corresponds to the position of that item in the array. The majority of languages define the starting index of the array as 0.
每个数据元素都被分配一个称为Index的正数值,它对应于该项在数组中的位置。 大多数语言将数组的起始索引定义为0。
The following are the two types of arrays:
以下是两种类型的数组:
We are all familiar with the famous Undo option, which is present in almost every application. Ever wondered how it works? The idea: you store the previous states of your work (which are limited to a specific number) in the memory in such an order that the last one appears first. This can’t be done just by using arrays. That is where the Stack comes in handy.
我们都熟悉著名的“ 撤消”选项,该选项几乎存在于每个应用程序中。 有没有想过它是如何工作的? 想法是:您将工作的先前状态(限于特定数量)存储在内存中,顺序是最后一个出现在最前面。 这不能仅仅通过使用数组来完成。 这就是堆栈派上用场的地方。
A real-life example of Stack could be a pile of books placed in a vertical order. In order to get the book that’s somewhere in the middle, you will need to remove all the books placed on top of it. This is how the LIFO (Last In First Out) method works.
堆叠的真实示例可能是一堆以垂直顺序放置的书。 为了获得位于中间位置的书,您需要删除放在其顶部的所有书。 这就是LIFO(后进先出)方法的工作方式。
Here’s an image of stack containing three data elements (1, 2 and 3), where 3 is at the top and will be removed first:
这是包含三个数据元素(1、2和3)的堆栈图像,其中3在顶部,并且将首先删除:
Basic operations of stack:
堆栈的基本操作:
Similar to Stack, Queue is another linear data structure that stores the element in a sequential manner. The only significant difference between Stack and Queue is that instead of using the LIFO method, Queue implements the FIFO method, which is short for First in First Out.
与Stack类似,Queue是另一个线性数据结构,该结构以顺序方式存储元素。 堆栈和队列之间唯一的显着区别是,队列使用FIFO实现而不使用LIFO方法 方法,是先进先出的缩写。
A perfect real-life example of Queue: a line of people waiting at a ticket booth. If a new person comes, they will join the line from the end, not from the start — and the person standing at the front will be the first to get the ticket and hence leave the line.
排队的一个完美的现实例子:排队的人在售票亭等待。 如果有新人来,他们将从头开始而不是从一开始就加入队伍—站在最前面的人将是第一个获得票证并因此离开队伍的人。
Here’s an image of Queue containing four data elements (1, 2, 3 and 4), where 1 is at the top and will be removed first:
这是包含四个数据元素(1、2、3和4)的Queue图像,其中1在顶部,并且将首先删除:
A linked list is another important linear data structure which might look similar to arrays at first but differs in memory allocation, internal structure and how basic operations of insertion and deletion are carried out.
链表是另一个重要的线性数据结构,乍一看可能与数组相似,但是在内存分配,内部结构以及插入和删除的基本操作上有所不同。
A linked list is like a chain of nodes, where each node contains information like data and a pointer to the succeeding node in the chain. There’s a head pointer, which points to the first element of the linked list, and if the list is empty then it simply points to null or nothing.
链表就像一个节点链,其中每个节点都包含诸如数据之类的信息以及指向链中后续节点的指针。 有一个头指针,它指向链接列表的第一个元素,如果列表为空,则仅指向null或不指向任何内容。
Linked lists are used to implement file systems, hash tables, and adjacency lists.
链接列表用于实现文件系统,哈希表和邻接列表。
Here’s a visual representation of the internal structure of a linked list:
这是链表内部结构的直观表示:
Following are the types of linked lists:
以下是链接列表的类型:
InsertAtEnd — Inserts a given element at the end of the linked list
InsertAtEnd —在链表的末尾插入给定元素
InsertAtHead — Inserts a given element at the start/head of the linked list
InsertAtHead —在链接列表的开头/开头插入给定元素
Delete — Deletes a given element from the linked list
删除 -从链接列表中删除给定元素
DeleteAtHead — Deletes the first element of the linked list
DeleteAtHead —删除链接列表的第一个元素
Search — Returns the given element from a linked list
搜索 -从链接列表中返回给定的元素
isEmpty — Returns true if the linked list is empty
isEmpty —如果链接列表为空,则返回true
A graph is a set of nodes that are connected to each other in the form of a network. Nodes are also called vertices. A pair(x,y) is called an edge, which indicates that vertex x is connected to vertex y. An edge may contain weight/cost, showing how much cost is required to traverse from vertex x to y.
图是一组以网络形式相互连接的节点。 节点也称为顶点。 pair(x,y)称为edge ,它指示顶点x连接到顶点y 。 一条边可能包含权重/成本,表示从顶点x到y遍历需要多少成本。
Types of Graphs:
图的类型:
In a programming language, graphs can be represented using two forms:
在编程语言中,图形可以使用两种形式表示:
Common graph traversing algorithms:
常见的图遍历算法:
A tree is a hierarchical data structure consisting of vertices (nodes) and edges that connect them. Trees are similar to graphs, but the key point that differentiates a tree from the graph is that a cycle cannot exist in a tree.
树是由顶点(节点)和连接它们的边组成的分层数据结构。 树类似于图,但是区别图与树的关键是树中不存在循环。
Trees are extensively used in Artificial Intelligence and complex algorithms to provide an efficient storage mechanism for problem-solving.
树在人工智能和复杂算法中得到了广泛使用,以提供用于解决问题的有效存储机制。
Here’s an image of a simple tree, and basic terminologies used in tree data structure:
这是一棵简单的树的图像,以及树数据结构中使用的基本术语:
The following are the types of trees:
以下是树的类型:
Out of the above, Binary Tree and Binary Search Tree are the most commonly used trees.
其中,二叉树和二叉搜索树是最常用的树。
Trie, which is also known as “Prefix Trees”, is a tree-like data structure which proves to be quite efficient for solving problems related to strings. It provides fast retrieval, and is mostly used for searching words in a dictionary, providing auto suggestions in a search engine, and even for IP routing.
Trie,也称为“前缀树”,是一种类似树的数据结构,被证明对于解决与字符串有关的问题非常有效。 它提供了快速的检索功能,主要用于在字典中搜索单词,在搜索引擎中提供自动建议,甚至用于IP路由。
Here’s an illustration of how three words “top”, “thus”, and “their” are stored in Trie:
这是Trie中三个单词“ top”,“ thus”和“ their”的存储方式的说明:
The words are stored in the top to the bottom manner where green colored nodes “p”, “s” and “r” indicates the end of “top”, “thus”, and “their” respectively.
单词以从上到下的方式存储,其中绿色节点“ p”,“ s”和“ r”分别表示“ top”,“ thus”和“ their”的结尾。
Commonly asked Trie interview questions:
特里采访常见问题:
Hashing is a process used to uniquely identify objects and store each object at some pre-calculated unique index called its “key.” So, the object is stored in the form of a “key-value” pair, and the collection of such items is called a “dictionary.” Each object can be searched using that key. There are different data structures based on hashing, but the most commonly used data structure is the hash table.
散列是用于唯一标识对象并将每个对象存储在一些预先计算的唯一索引(称为“键”)的过程。 因此,对象以“键值”对的形式存储,此类项目的集合称为“字典”。 可以使用该键搜索每个对象。 基于哈希的数据结构不同,但是最常用的数据结构是哈希表 。
Hash tables are generally implemented using arrays.
哈希表通常使用数组来实现。
The performance of hashing data structure depends upon these three factors:
哈希数据结构的性能取决于以下三个因素:
Here’s an illustration of how the hash is mapped in an array. The index of this array is calculated through a Hash Function.
这是散列如何在数组中映射的说明。 该数组的索引是通过哈希函数计算的。
The above are the top eight data structures that you should definitely know before walking into a coding interview.
以上是进入编码面试之前您绝对应该知道的八大数据结构。
If you are looking for resources on data structures for coding interviews, look at the interactive & challenge based courses: Data Structures for Coding Interviews (Python, Java, or JavaScript).
如果您正在寻找有关编码面试的数据结构的资源,请参阅基于交互和挑战的课程: 编码面试的数据结构 ( Python , Java或JavaScript )。
For more advanced questions, look at Coderust 3.0: Faster Coding Interview Preparation with Interactive Challenges & Visualizations.
有关更多高级问题,请参阅Coderust 3.0:具有交互式挑战和可视化的更快的编码面试准备 。
If you are preparing for a software engineering interviews, here’s a comprehensive roadmap to prepare for coding Interviews.
如果您正在准备进行软件工程面试,那么这里是准备编写面试代码的综合路线图 。
Good luck and happy learning! :)
祝你好运,学习愉快! :)
数据结构面试题编程题
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。