Missing number(s) Interview Question Redux

后端 未结 8 773
轮回少年
轮回少年 2021-01-30 07:14

The common interview problem of determining the missing value in a range from 1 to N has been done a thousand times over. Variations include 2 missing values up to K missing val

8条回答
  •  清酒与你
    2021-01-30 07:55

    My question is that seeing as the [...] cases converge at roughly something larger than O(nlogn) [...]

    In 2011 (after you posted this question) Caf posted a simple answer that solves the problem in O(n) time and O(k) space [where the array size is n - k].

    Importantly, unlike in other solutions, Caf's answer has no hidden memory requirements (using bit array's, adding numbers to elements, multiplying elements by -1 - these would all require O(log(n)) space).

    Note: The question here (and the original question) didn't ask about the streaming version of the problem, and the answer here doesn't handle that case.


    Regarding the other answers: I agree that many of the proposed "solutions" to this problem have dubious complexity claims, and if their time complexities aren't better in some way than either:

    • count sort (O(n) time and space)
    • compare (heap) sort (O(n*log(n)) time, O(1) space)

    ...then you may as well just solve the problem by sorting.

    However, we can get better complexities (and more importantly, genuinely faster solutions):

提交回复
热议问题