数据结构面试题编程题_您下次编程面试时应该了解的顶级数据结构

数据结构面试题编程题

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.

即使您只是想在当前工作中变得更好,学习数据结构也是必不可少的。 让我们从了解基础开始。

什么是数据结构? (What is a Data Structure?)

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.

简而言之,数据结构是一个以特定布局存储数据的容器。 这种“布局”使数据结构在某些操作中有效,而在另一些操作中效率低下。 您的目标是了解数据结构,以便选择最适合当前问题的数据结构。

为什么我们需要数据结构? (Why do we need Data Structures?)

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.

根据不同的场景,数据需要以特定的格式存储。 我们有一些数据结构可以满足我们以不同格式存储数据的需求。

常用数据结构 (Commonly used Data Structures)

Let’s first list the most commonly used data structures, and then we’ll cover them one by one:

让我们首先列出最常用的数据结构,然后逐一介绍它们:

  1. Arrays

    数组
  2. Stacks

    堆栈
  3. Queues

    Queue列
  4. Linked Lists

    链表
  5. Trees

    树木
  6. Graphs

    图表
  7. Tries (they are effectively trees, but it’s still good to call them out separately).

    尝试(它们实际上是树,但是将它们分别喊出还是不错的)。
  8. Hash Tables

    哈希表

数组 (Arrays)

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:

以下是两种类型的数组:

  • One-dimensional arrays (as shown above)

    一维数组(如上所示)
  • Multi-dimensional arrays (arrays within arrays)

    多维数组(数组中的数组)
阵列的基本操作 (Basic Operations on Arrays)
  • Insert — Inserts an element at a given index

    插入—在给定索引处插入元素
  • Get — Returns the element at a given index

    Get —返回给定索引处的元素
  • Delete — Deletes an element at a given index

    删除-删除给定索引处的元素
  • Size — Gets the total number of elements in an array

    Size —获取数组中元素的总数
Array面试常见问题 (Commonly asked Array interview questions)
  • Find the second minimum element of an array

    查找数组的第二个最小元素
  • First non-repeating integers in an array

    数组中的第一个非重复整数
  • Merge two sorted arrays

    合并两个排序的数组
  • Rearrange positive and negative values in an array

    重新排列数组中的正值和负值

堆栈 (Stacks)

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:

堆栈的基本操作:

  • Push — Inserts an element at the top

    推入—在顶部插入一个元素
  • Pop — Returns the top element after removing from the stack

    Pop —从堆栈中删除后返回顶部元素
  • isEmpty — Returns true if the stack is empty

    isEmpty —如果堆栈为空,则返回true
  • Top — Returns the top element without removing from the stack

    Top —返回顶部元素,而不从堆栈中移除
常见的Stack面试问题 (Commonly asked Stack interview questions)
  • Evaluate postfix expression using a stack

    使用堆栈评估后缀表达式
  • Sort values in a stack

    对堆栈中的值进行排序
  • Check balanced parentheses in an expression

    检查表达式中的平衡括号

Queue列 (Queues)

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在顶部,并且将首先删除:

队列的基本操作 (Basic operations of Queue)
  • Enqueue() — Inserts an element to the end of the queue

    Enqueue()—将元素插入队列的末尾
  • Dequeue() — Removes an element from the start of the queue

    Dequeue()-从队列的开头删除一个元素
  • isEmpty() — Returns true if the queue is empty

    isEmpty()—如果队列为空,则返回true
  • Top() — Returns the first element of the queue

    Top()—返回队列的第一个元素
排队面试常见问题 (Commonly asked Queue interview questions)
  • Implement stack using a queue

    使用队列实现堆栈
  • Reverse first k elements of a queue

    反转队列的前k个元素
  • Generate binary numbers from 1 to n using a queue

    使用队列生成从1到n的二进制数

链表 (Linked List)

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:

以下是链接列表的类型:

  • Singly Linked List (Unidirectional)

    单链表(单向)
  • Doubly Linked List (Bi-directional)

    双链表(双向)
链表的基本操作: (Basic operations of Linked List:)
  • 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

常见问题链表面试问题 (Commonly asked Linked List interview questions)
  • Reverse a linked list

    反向链接列表
  • Detect loop in a linked list

    检测链表中的循环
  • Return Nth node from the end in a linked list

    从链表的末尾返回第N个节点
  • Remove duplicates from a linked list

    从链接列表中删除重复项

图表 (Graphs)

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:

图的类型:

  • Undirected Graph

    无向图
  • Directed Graph

    有向图

In a programming language, graphs can be represented using two forms:

在编程语言中,图形可以使用两种形式表示:

  • Adjacency Matrix

    邻接矩阵
  • Adjacency List

    邻接表

Common graph traversing algorithms:

常见的图遍历算法:

  • Breadth First Search

    广度优先搜索
  • Depth First Search

    深度优先搜索
Graph面试常见问题 (Commonly asked Graph interview questions)
  • Implement Breadth and Depth First Search

    实施广度和深度优先搜索
  • Check if a graph is a tree or not

    检查图是否为树
  • Count the number of edges in a graph

    计算图中的边数
  • Find the shortest path between two vertices

    查找两个顶点之间的最短路径

树木 (Trees)

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:

以下是树的类型:

  • N-ary Tree

    一元树
  • Balanced Tree

    平衡树
  • Binary Tree

    二叉树
  • Binary Search Tree

    二进制搜索树
  • AVL Tree

    AVL树
  • Red Black Tree

    红黑树
  • 2–3 Tree

    2–3树

Out of the above, Binary Tree and Binary Search Tree are the most commonly used trees.

其中,二叉树和二叉搜索树是最常用的树。

常见问题树面试问题 (Commonly asked Tree interview questions)
  • Find the height of a binary tree

    查找二叉树的高度
  • Find kth maximum value in a binary search tree

    在二叉搜索树中找到第k个最大值
  • Find nodes at “k” distance from the root

    查找距根“ k”距离的节点
  • Find ancestors of a given node in a binary tree

    在二叉树中查找给定节点的祖先

特里 (Trie)

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:

特里采访常见问题:

  • Count total number of words in Trie

    计算Trie中的单词总数
  • Print all words stored in Trie

    打印所有存储在Trie中的单词
  • Sort elements of an array using Trie

    使用Trie排序数组的元素
  • Form words from a dictionary using Trie

    使用Trie从字典中套用单词
  • Build a T9 dictionary

    建立T9字典

哈希表 (Hash Table)

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:

哈希数据结构的性能取决于以下三个因素:

  • Hash Function

    散列函数
  • Size of the Hash Table

    哈希表的大小
  • Collision Handling Method

    碰撞处理方法

Here’s an illustration of how the hash is mapped in an array. The index of this array is calculated through a Hash Function.

这是散列如何在数组中映射的说明。 该数组的索引是通过哈希函数计算的。

散列面试常见问题 (Commonly asked Hashing interview questions)
  • Find symmetric pairs in an array

    在数组中查找对称对
  • Trace complete path of a journey

    追踪完整的旅程
  • Find if an array is a subset of another array

    查找一个数组是否是另一个数组的子集
  • Check if given arrays are disjoint

    检查给定数组是否不相交

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).

如果您正在寻找有关编码面试的数据结构的资源,请参阅基于交互和挑战的课程: 编码面试的数据结构 ( PythonJavaJavaScript )。

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! :)

祝你好运,学习愉快! :)

翻译自: https://www.freecodecamp.org/news/the-top-data-structures-you-should-know-for-your-next-coding-interview-36af0831f5e3/

数据结构面试题编程题

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值