Easy interview question got harder: given numbers 1..100, find the missing number(s) given exactly k are missing

前端 未结 30 1214
时光说笑
时光说笑 2020-11-22 07:02

I had an interesting job interview experience a while back. The question started really easy:

Q1: We have a bag containing numbers

30条回答
  •  有刺的猬
    2020-11-22 08:02

    Very nice problem. I'd go for using a set difference for Qk. A lot of programming languages even have support for it, like in Ruby:

    missing = (1..100).to_a - bag
    

    It's probably not the most efficient solution but it's one I would use in real life if I was faced with such a task in this case (known boundaries, low boundaries). If the set of number would be very large then I would consider a more efficient algorithm, of course, but until then the simple solution would be enough for me.

提交回复
热议问题