数据结构面试题编程题

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. QueuesQueue列
  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 indexGet —返回给定索引处的元素
  • Delete — Deletes an element at a given index删除-删除给定索引处的元素
  • Size — Gets the total number of elements in an arraySize —获取数组中元素的总数

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 stackPop —从堆栈中删除后返回顶部元素
  • isEmpty — Returns true if the stack is emptyisEmpty —如果堆栈为空,则返回true
  • Top — Returns the top element without removing from the stackTop —返回顶部元素,而不从堆栈中移除

常见的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 queueEnqueue()—将元素插入队列的末尾
  • Dequeue() — Removes an element from the start of the queueDequeue()-从队列的开头删除一个元素
  • isEmpty() — Returns true if the queue is emptyisEmpty()—如果队列为空,则返回true
  • Top() — Returns the first element of the queueTop()—返回队列的第一个元素

排队面试常见问题 (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 TreeAVL树
  • Red Black Tree红黑树
  • 2–3 Tree2–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).

如果您正在寻找有关编码面试的数据结构的资源,请参阅基于交互和挑战的课程: 编码面试的数据结构 ( 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! :)

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

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

数据结构面试题编程题

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

  1. java 编程题_最新JAVA编程题全集(50题及答案)92862

    <最新JAVA编程题全集(50题及答案)92862>由会员分享,可在线阅读,更多相关<最新JAVA编程题全集(50题及答案)92862(32页珍藏版)>请在人人文库网上搜索. ...

  2. pta中java编程题_多文件编程题

    多文件编程题与函数题相似,区别是裁判编写的判题程序可能涉及多个文件,因此不是写在题干里,而是以附件的形式供学生下载,方便学生调试.学生须按照题干上给出的要求编写程序,完成指定功能.学生的提交也可能包含 ...

  3. c++经典编程题_全国青少年软件编程等级考试C语言经典程序题10道十

    全国青少年软件编程等级考试C语言经典程序题10道十 [程序91] 题目:时间函数举例1 1.程序分析: 2.程序源代码: #include "stdio.h" #include & ...

  4. python关于文件的编程题_《Python编程》源代码文件

    压缩包 : bbc59749e0028c3f1ab3a1c9a762a6d.rar 列表 <Python编程>源代码文件/.gitignore <Python编程>源代码文件/ ...

  5. java逻辑编程题_用Java编程解决一道逻辑推理题

    package mytest; import java.util.Scanner; public class Test14 { /** * 竞赛结果表明,他们都说对了一半,说错了一半,并且无并列名次, ...

  6. pythonmooc期末考试编程题_大学moocPython编程基础期末考试搜题公众号答案

    什么叫首饰? 确保采煤工作面支架的初撑力符合要求的方法包括(). 简述广播媒介及广播广告的局限. "字段映象"选项卡有什么作用?除了在"字段映象"选项卡上以外, ...

  7. python数据结构编程题_生信编程实战第5题(python)

    image.png 先从hg38的gtf中提取"ANXA1"基因grep '"ANXA1"' hg38.gtf >ANXA1.gtf 在题目之前先分析要处 ...

  8. 大一python编程题_请教python编程问题(作业就剩这几道题了)

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 1. def cleanword(word): (用Python写出程序,使程序可以通过下面的doctest) """ &g ...

  9. 【100道面试题真题讲解】C++面试题讲解+JAVA面试题讲解+Linux面试题讲解+数据结构面试题+计算机网络面试题 讲解视频-持续更新中

    最近找到了一个非常好的公众号:IT笔试面试真题讲解,每天视频分享一道IT公司面试高频题目,完全免费哦,非常适合找工作的学生复习+总结+提炼. 白嫖不敢独吞,分享给大家,也给作者增加一点访问量,鼓励作者 ...

最新文章

  1. 由“公共类”看出的一些东西
  2. pycharm连接远程服务器
  3. ul ol li的序号编号样式
  4. 白话Elasticsearch48-深入聚合数据分析之 Percentiles Aggregation-percentiles百分比算法以及网站访问时延统计及Percentiles优化
  5. 火狐已阻止载入混合活动内容“http://www.XXX/index.php?app=serviceac=authts=isauthurl=...
  6. Xming + PuTTY 在Windows下远程Linux主机使用图形界面的程序
  7. Mozilla开发全新的公开网络API WebXR 来实现增强现实
  8. C++(STL):27 ---关联式容器set源码剖析
  9. Jquery 动画
  10. Sqlmap安装教程
  11. 中国大陆新身份证号码算法
  12. 双显示器无法加载第二个
  13. html的长度单位的选择,html中常见长度单位有哪些?
  14. java基础编程题_Java基础练习题:编程练习(1) - 菜鸟头头
  15. matlab的特殊字符(上下标和希腊字母等)
  16. 安卓手机用AidLux安装Linux免Root,安装到Debian 10不能安装docker
  17. Neo4j ① <图论>图,节点,关系,属性<知识图谱和图库>图谱,图库,优势<基础>模块,应用场景,环境搭建,浏览器
  18. Docker之API操作
  19. SDK Manager安装
  20. 使用LocalDate获取本周周一和周日

热门文章

  1. Mybatis注解学习记录
  2. 实现对学生表的删除操作
  3. hitTest和pointInside方法
  4. 全网把Map中的hash()分析的最透彻的文章,别无二家。
  5. 面向对象进阶2 组合
  6. MySQL ERROR 1878 解决办法
  7. Educational Codeforces Round 9 F. Magic Matrix 最小生成树
  8. 在 VMware ESXi 5.0 上安装万兆网卡驱动
  9. Java传输对象模式
  10. 怎样在swift中创建CocoaPods