容易的面试问题变得更加困难:给定数字1..100,在正好缺少k的情况下,找到缺失的数字

狂风中的少年 提交于 2020-10-19 04:36:22

问题:

I had an interesting job interview experience a while back. 前一段时间我有一次有趣的面试经历。 The question started really easy: 这个问题开始很容易:

Q1 : We have a bag containing numbers 1 , 2 , 3 , …, 100 . Q1:我们有一个包含数字的包123 ,..., 100 Each number appears exactly once, so there are 100 numbers. 每个数字仅出现一次,因此有100个数字。 Now one number is randomly picked out of the bag. 现在,从袋子中随机抽取一个号码。 Find the missing number. 查找丢失的号码。

I've heard this interview question before, of course, so I very quickly answered along the lines of: 我当然已经听过这个面试问题,所以我很快就回答了以下问题:

A1 : Well, the sum of the numbers 1 + 2 + 3 + … + N is (N+1)(N/2) (see Wikipedia: sum of arithmetic series ). A1 :恩,数字1 + 2 + 3 + … + N的总和为(N+1)(N/2) (请参阅Wikipedia:算术级数之和 )。 For N = 100 , the sum is 5050 . 对于N = 100 ,总和为5050

Thus, if all numbers are present in the bag, the sum will be exactly 5050 . 因此,如果袋子中所有数字都存在,则总和为5050 Since one number is missing, the sum will be less than this, and the difference is that number. 由于缺少一个数字,所以总和小于这个数字,而差就是那个数字。 So we can find that missing number in O(N) time and O(1) space. 因此我们可以找到O(N)时间和O(1)空间中的缺失数。

At this point I thought I had done well, but all of a sudden the question took an unexpected turn: 在这一点上,我认为我做得不错,但是突然之间,这个问题突然发生了变化:

Q2 : That is correct, but now how would you do this if TWO numbers are missing? Q2 :是的,但是现在如果缺少两个数字,您将如何处理?

I had never seen/heard/considered this variation before, so I panicked and couldn't answer the question. 我之前从未见过/听过/考虑过这种变化,所以我感到惊慌,无法回答这个问题。 The interviewer insisted on knowing my thought process, so I mentioned that perhaps we can get more information by comparing against the expected product, or perhaps doing a second pass after having gathered some information from the first pass, etc, but I really was just shooting in the dark rather than actually having a clear path to the solution. 面试官坚持要知道我的思维过程,所以我提到也许我们可以通过与预期产品进行比较来获得更多信息,或者也许在从第一遍收集到一些信息之后再进行第二遍,等等,但是我真的只是在拍摄在黑暗中,而不是真正找到解决方案的清晰途径。

The interviewer did try to encourage me by saying that having a second equation is indeed one way to solve the problem. 面试官的确通过说第二个方程式确实是解决问题的一种方式来鼓励我。 At this point I was kind of upset (for not knowing the answer before hand), and asked if this is a general (read: "useful") programming technique, or if it's just a trick/gotcha answer. 在这一点上,我有点不高兴(因为事先不知道答案),并询问这是否是一种通用的(读作:“有用的”)编程技术,还是仅仅是一个技巧/陷阱。

The interviewer's answer surprised me: you can generalize the technique to find 3 missing numbers. 面试官的回答让我感到惊讶:您可以推广该技术以找到3个缺失的数字。 In fact, you can generalize it to find k missing numbers. 实际上,您可以对其进行概括以找到k个缺失数字。

Qk : If exactly k numbers are missing from the bag, how would you find it efficiently? Qk :如果袋子中恰好缺少k个数字,您将如何有效地找到它?

This was a few months ago, and I still couldn't figure out what this technique is. 这是几个月前,但我仍然不知道这种技术是什么。 Obviously there's a Ω(N) time lower bound since we must scan all the numbers at least once, but the interviewer insisted that the TIME and SPACE complexity of the solving technique (minus the O(N) time input scan) is defined in k not N . 显然,存在一个Ω(N)时间下限,因为我们必须至少扫描一次所有数字,但是访调员坚持认为,求解技术的TIMESPACE复杂度(减去O(N)时间输入扫描)定义为k不是N。

So the question here is simple: 所以这里的问题很简单:

  • How would you solve Q2 ? 您将如何解决Q2
  • How would you solve Q3 ? 您将如何解决第3季度
  • How would you solve Qk ? 您将如何解决Qk

Clarifications 澄清说明

  • Generally there are N numbers from 1.. N , not just 1..100. 通常,从1 .. N开始N个数字,而不仅仅是1..100。
  • I'm not looking for the obvious set-based solution, eg using a bit set , encoding the presence/absence each number by the value of a designated bit, therefore using O(N) bits in additional space. 我不是在寻找明显的基于集合的解决方案,例如使用位集 ,通过指定位的值编码每个数字的存在/不存在,因此在其他空间中使用O(N)位。 We can't afford any additional space proportional to N . 我们无法承受与N成正比的任何额外空间。
  • I'm also not looking for the obvious sort-first approach. 我也不在寻找明显的排序优先方法。 This and the set-based approach are worth mentioning in an interview (they are easy to implement, and depending on N , can be very practical). 这种方法和基于集合的方法在采访中值得一提(它们易于实现,并且取决于N ,可能非常实用)。 I'm looking for the Holy Grail solution (which may or may not be practical to implement, but has the desired asymptotic characteristics nevertheless). 我正在寻找“圣杯”解决方案(可能实现或可能不实际,但仍具有所需的渐近特性)。

So again, of course you must scan the input in O(N) , but you can only capture small amount of information (defined in terms of k not N ), and must then find the k missing numbers somehow. 同样,当然,您必须扫描O(N)的输入,但是您只能捕获少量信息(以k而不是N定义),然后必须以某种方式找到k个缺失的数字。


解决方案:

参考一: https://stackoom.com/question/EeVS/容易的面试问题变得更加困难-给定数字-在正好缺少k的情况下-找到缺失的数字
参考二: https://oldbug.net/q/EeVS/Easy-interview-question-got-harder-given-numbers-1-100-find-the-missing-number-s-given-exactly-k-are-missing
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!