In FAANG company interview, Candidates always come across heap problems. There is one question they do like to ask — Top K. Because these companies have a huge dataset and they can’t always go through all the data. Finding tope data is always a good option. In this article, I would like to share my methodology on handling heap interview problems. Also, I’ll share 2 typical heap algorithm problems — Top K Frequent, Merge K arrays.

在FAANG公司面试中,候选人总是遇到堆砌问题。 他们想问一个问题-TopK。因为这些公司拥有庞大的数据集,而且不能总是遍历所有数据。 查找tope数据始终是一个不错的选择。 在本文中,我想分享我的方法来处理堆访谈问题。 另外,我将分享2个典型的堆算法问题-排在前K个,合并K个数组。

什么是堆? (What is Heap?)

Heap is a complete Tree-like data structure. It has a parent node, left child node and right child node. For the interview, the min-heap and max-heap are very common to see.

堆是一个完整的树状数据结构。 它具有父节点,左子节点和右子节点。 对于采访来说,最小堆和最大堆很常见。

Max-Heap: In a Max-Heap the value present at the parent node must be greatest among the values present at all of its children.

最大堆 :在最大堆中,父节点上存在的值必须在其所有子节点上存在的值中最大。

Min-Heap: In a Min-Heap the value present at the parent node must be minimum among the values present at all of its children.

最小堆 :在最小堆中,父节点上存在的值必须在其所有子节点上存在的值中最小。

堆方法 (Heap methodology)

This is the methodology to help think about the solution:

这是帮助考虑解决方案的方法:

Image by author图片作者

For most of the popular data structure problems. The feature is common: we have to do sort-like steps. We can sort all the elements: heapsort. We can sort just several elements: top-k. The elements should be comparable. Otherwise, we may have to push some comparable value along with the elements.

对于大多数流行的数据结构问题。 该功能很常见:我们必须执行类似排序的步骤。 我们可以对所有元素进行排序:heapsort。 我们可以对几个元素进行排序:top-k。 元素应该是可比较的。 否则,我们可能不得不将一些可比较的价值与要素一起推向市场。

Push: What we push some comparable elements such as int. Sometimes I have to push a comparable object. No matter what we push, we are doing it for comparison and heapify.

推送 :我们推送一些可比较的元素,例如int。 有时我必须推动一个类似的对象。 无论我们推动什么,我们都在进行比较和堆化。

Heapify: Heapify is the process of converting a binary tree into a Heap data structure by rearranging the nodes in the binary tree to a max-heap or min-heap.

Heapify :Heapify是通过将二叉树中的节点重新排列为max-heap或min-heap来将二叉树转换为Heap数据结构的过程。

In case one of your interview questions is implementing heapify, Here I attached the code:

如果您的面试问题之一是实现heapify,请在此处附加代码:

In this case, we have to push the node to the heap. As heap’s representation is an array. It appends the new node to the end and heapify it. Here is the code for a max-heap:

在这种情况下,我们必须将节点推送到堆。 由于堆的表示是一个数组。 它将新节点附加到末尾并对其进行堆放。 这是最大堆的代码:

For each insert, it takes O (log K) time to heapify the node, K is the heap size.

对于每个插入,需要O(log K)时间来堆满节点,K是堆大小。

Pop: Now it’s time to get what we want. Here we have two cases:

Pop :现在是时候获得我们想要的东西了。 这里有两种情况:

  • We use the value on the root of the heap. If needed, we can use heap[0] to check the top one if we have some extra conditions in the question. After we get the value, we can put it in an array or do extra work as the question requested.

    我们在堆的根上使用该值。 如果需要,可以在问题中使用一些额外的条件,使用heap [0]检查最上面的一个。 获得值后,我们可以将其放入数组中,也可以根据要求进行其他工作。

  • In other cases, we need the rest of the values in the Heap. We can loop through the heap and return it.在其他情况下,我们需要堆中的其余值。 我们可以遍历堆并返回它。

实践 (Practices)

Here I provide solutions with python. For most of the Algorithm problems, it’s not just one solution. It’s better to give different solutions, analyse the time/space complexity and move to the best one.

在这里,我提供了python解决方案。 对于大多数算法问题,这不仅仅是一种解决方案。 最好提供不同的解决方案,分析时间/空间的复杂性,然后转向最佳解决方案。

In python, we have heapq library to help us operate the heap properly. It has nlargest and nsmallest which we can use to solve top-k type problem in one line of code. However, to ensure you can understand better about Heap, let’s go step by step.

在python中,我们有heapq库来帮助我们正确地操作堆。 它具有nlargensmallest ,可用于在一行代码中解决top-k类型的问题。 但是,为了确保您可以更好地了解Heap,让我们逐步进行。

前K频 (Top K Frequent)

Here is the reference question on leetcode. We just find the top K frequent values in an array.

这是有关leetcode的参考问题 。 我们只是在数组中找到前K个频繁值。

Here are the steps from the code:

这是代码中的步骤:

  • Convert the array to a new array where the element is a tuple with value and frequency.将数组转换为新数组,其中元素是具有值和频率的元组。
  • Push the tuple elements to the k-size heap. The heap heapify automatically whenever a new node has been pushed.将元组元素推入k大小的堆。 每当推送新节点时,堆都会自动堆化。
  • After we go through all the elements with the heap, we loop the rest of the heap to find our target values.在遍历了堆中的所有元素之后,我们循环其余堆以找到目标值。

If you are pushing an object, you have to ensure the object is comparable. If you push a tuple, the heap compare the first element while doing heapify, if ties happened, it will compare the next value.

如果要推动对象,则必须确保该对象具有可比性。 如果推入一个元组,堆将在执行heapify时比较第一个元素如果发生联系,它将比较下一个值

合并K个数组 (Merge K arrays)

Here is the reference question on leetcode. We have to Merge k sorted linked lists and return it as one sorted list. Here is the solution with Heap:

这是有关leetcode的参考问题 。 我们必须合并k个排序的链表,并将其作为一个排序表返回。 这是堆的解决方案:

Here are the steps from the code:

这是代码中的步骤:

  • We build an array with the first node and its value from each ListNode in lists. Then we heapify it to be a min-heap.我们使用列表中每个ListNode的第一个节点及其值构建一个数组。 然后我们将其堆成最小堆。
  • We use the peek node in the heap to build the output ListNode.我们使用堆中的peek节点构建输出ListNode。
  • If the node not ends, we use heapreplace which means we pop and return the smallest item from the heap, and also push the new item.

    如果节点未结束,则使用heapreplace,这意味着我们从堆中弹出并返回最小的项,并推送新

Here I would like to explain another solution to this question with the Priority Queue.

在这里,我想用“优先级队列”解释这个问题的另一种解决方案。

Priority Queues are abstract data structures where each data/value in the queue has a certain priority. Priority Queue is an extension of the queue with the following properties.1, An element with high priority is dequeued before an element with low priority.2, If two elements have the same priority, they are served according to their order in the queue.

优先级队列是抽象的数据结构,其中队列中的每个数据/值都有特定的优先级。 Priority Queue是具有以下属性的队列的扩展:1,将高优先级的元素排在优先级低的元素之前。2,如果两个元素具有相同的优先级,则根据它们在队列中的顺序进行服务。

Here are the steps from the code:

这是代码中的步骤:

  • From each element in the List, we find the first node with its value in the ListNode, put it into the Priority Queue. We need the node value because we need it for comparison in the Priority Queue.从List中的每个元素中,我们在ListNode中找到具有其值的第一个节点,并将其放入Priority Queue。 我们需要节点值,因为在优先级队列中需要进行比较。
  • Then, we get the element from the Priority Queue (the element with the smallest value), link it to the output ListNode.然后,我们从Priority Queue中获得元素(值最小的元素),并将其链接到输出ListNode。
  • We move the node to the next, if it exists, we put it into the Priority Queue. After all these steps, we return the output ListNode which is the head.next.我们将节点移到下一个节点(如果存在),将其放入优先级队列。 完成所有这些步骤之后,我们返回输出ListNode,它是head.next。

最后的话 (Final Words)

After you read this article, you may think heap is not as hard as you thought with this methodology. However, we may have other data structure comes up with Heap such as the LinkedList. Therefore, we still need to practise and master the basic data structure API with the language you preferred.

阅读本文之后,您可能会认为堆并不像您对这种方法所想象的那样难。 但是,Heap可能还会提供其他数据结构,例如LinkedList。 因此,我们仍然需要使用您喜欢的语言来练习和掌握基本的数据结构API。

Hope you learn something from this article. If you are interested in reading any of my other articles, you are welcome to check them out in my profile. Best wishes for your job hunting!

希望您能从本文中学到一些东西。 如果您有兴趣阅读我的其他文章,欢迎您在我的个人资料中查看。 祝您工作愉快!

翻译自: https://medium.com/dev-genius/a-methodology-to-ace-the-heap-algorithms-question-c7063b2ba7fd


http://www.taodudu.cc/news/show-994989.html

相关文章:

  • itchat 道歉_人类的“道歉”
  • 数据科学 python_为什么需要以数据科学家的身份学习Python的7大理由
  • 动量策略 python_在Python中使用动量通道进行交易
  • 高斯模糊为什么叫高斯滤波_为什么高斯是所有发行之王?
  • 从Jupyter Notebook到脚本
  • 加勒比海兔_加勒比海海洋物种趋势
  • srpg 胜利条件设定_英雄联盟获胜条件
  • 机器学习 综合评价_PyCaret:机器学习综合
  • 盛严谨,严谨,再严谨。_评估员工调查的统计严谨性
  • arima 预测模型_预测未来:学习使用Arima模型进行预测
  • bigquery_在BigQuery中链接多个SQL查询
  • mysql 迁移到tidb_通过从MySQL迁移到TiDB来水平扩展Hive Metastore数据库
  • 递归函数基例和链条_链条和叉子
  • 足球预测_预测足球热
  • python3中朴素贝叶斯_贝叶斯统计:Python中从零开始的都会都市
  • 数据治理 主数据 元数据_我们对数据治理的误解
  • 提高机器学习质量的想法_如何提高机器学习的数据质量?
  • 逻辑回归 python_深入研究Python的逻辑回归
  • Matplotlib中的“ plt”和“ ax”到底是什么?
  • cayenne:用于随机模拟的Python包
  • spotify 数据分析_没有数据? 没问题! 如何从Wikipedia和Spotify收集重金属数据
  • kaggle数据集_Kaggle上有170万份ArXiv文章的数据集
  • 深度学习数据集中数据差异大_使用差异隐私来利用大数据并保留隐私
  • 小型数据库_如果您从事“小型科学”工作,那么您是否正在利用数据存储库?
  • 参考文献_参考
  • 数据统计 测试方法_统计测试:了解如何为数据选择最佳测试!
  • 每个Power BI开发人员的Power Query提示
  • a/b测试_如何进行A / B测试?
  • 面向数据科学家的实用统计学_数据科学家必知的统计数据
  • 在Python中有效使用JSON的4个技巧

在FAANG面试中破解堆算法相关推荐

  1. 微软面试中简单的算法题目(转)

    微软面试中简单的算法题目(转) (说明:这些题就不是什么花样了,考的是你的基础知识怎么样.再聪明而没有实学的人都将会被这些题所淘汰.)  1.链表和数组的区别在哪里? ANSWER 主要在基本概念上的 ...

  2. 排序算法 - 面试中的排序算法总结

    排序算法总结 查找和排序算法是算法的入门知识,其经典思想可以用于很多算法当中.因为其实现代码较短,应用较常见.所以在面试中经常会问到排序算法及其相关的问题.但万变不离其宗,只要熟悉了思想,灵活运用也不 ...

  3. 校招面试中常见的算法题整理【长文】

    ⭐️我叫恒心,一名喜欢书写博客的研究生在读生. 原创不易~转载麻烦注明出处,并告知作者,谢谢!!! 这是一篇近期会不断更新的博客欧~~~ 有什么问题的小伙伴 欢迎留言提问欧. 文章目录 前言 一.链表 ...

  4. 前端面试中常见的算法问题

    虽说我们很多时候前端很少有机会接触到算法.大多都交互性的操作,然而从各大公司面试来看,算法依旧是考察的一方面.实际上学习数据结构与算法对于工程师去理解和分析问题都是有帮助的.如果将来当我们面对较为复杂 ...

  5. 【深度学习】一文搞定面试中的优化算法

    深度学习各类优化器 借用古代炼丹的一些名词,我们可以把训练模型中的数据比做炼丹药材,模型比做炼丹炉,火候比做优化器.那么我们知道,同样的药材同样的炼丹炉,但是火候不一样的话,炼出来的丹药千差万别,同样 ...

  6. JS面试中常见的算法题

    js除了基础知识以外,算法也是挺重要的.因此特意整理了一些常见的算法题,希望大家有帮助. 1.验证一个数是否是素数 1.如果这个数是 2 或 3,一定是素数: function isPrime(num ...

  7. 数据结构 - 链表 - 面试中常见的链表算法题

    数据结构 - 链表 - 面试中常见的链表算法题 数据结构是面试中必定考查的知识点,面试者需要掌握几种经典的数据结构:线性表(数组.链表).栈与队列.树(二叉树.二叉查找树.平衡二叉树.红黑树).图. ...

  8. python算法题排序_python-数据结构与算法- 面试常考排序算法题-快排-冒泡-堆排-二分-选择等...

    算法可视化网站推荐---->visualgo 0.面试题中的排序算法 一些排序算法可能在工作中用的会比较少,但是面试却是不得不面对的问题.算法有助于提高我们对数据结构的理解以及提高自己的逻辑能力 ...

  9. 面试中的 10 大排序算法总结

    前言 查找和排序算法是算法的入门知识,其经典思想可以用于很多算法当中.因为其实现代码较短,应用较常见.所以在面试中经常会问到排序算法及其相关的问题.但万变不离其宗,只要熟悉了思想,灵活运用也不是难事. ...

最新文章

  1. 人工智能的浪潮中,知识图谱何去何从?
  2. shell编程中的 ${ }强大功能
  3. kafka--storm--mongodb
  4. Gartner表示:2017年全球IT支出上升2.7% 中国IT支出达到2.34万亿
  5. 05 | REST消息通信:如何使用 OpenFeign 简化服务间通信
  6. matlab system object,通过 System object 实现模块
  7. python 控制qq_最必要的最小建议集:写给刚入门编程(python)的同学
  8. xbox可以录视频声音吗_什么是Xbox Live Gold,它值得吗?
  9. Spark精华问答 | Spark和Hadoop的架构区别解读
  10. [转载] 整理下java中stringBuilder和stringBuffer两个类的区别
  11. The Dataflow Model: A Practical Approach to Balancing
  12. 受新冠病毒影响,谷歌延迟发布 Chrome 和 Chrome OS 安全更新版本
  13. 一种连续语音识别系统的制作方法
  14. c#随机数总结,汉字,英文,数字
  15. 页面中超长字段只显示部分
  16. 订单导出(淘宝天猫)
  17. simulink 菜单栏 不见了
  18. CH6803 导弹防御塔
  19. 赣州旅游职业学校学计算机,赣州旅游职业学校是公办的吗
  20. CTF的认识(勿喷,求饶)

热门文章

  1. vector和list容器有哪些区别
  2. Socket网络编程--小小网盘程序(3)
  3. 第一章 TCP/IP协议族
  4. 国内互联网公司算法机器学习岗(阿里星)面试总结
  5. OO_BLOG3_规格化设计(JML学习)
  6. Maven报错找不到jre
  7. myelcipse和maven搭建项目
  8. More DETAILS! PBR的下一个发展在哪里?
  9. docker在Centos上的安装
  10. 广度优先搜索(BFS)