Find the largest k numbers in k arrays stored across k machines

前端 未结 7 888
悲哀的现实
悲哀的现实 2020-12-28 19:34

This is an interview question. I have K machines each of which is connected to 1 central machine. Each of the K machines have an array of 4 byte numbers in file. You can use

7条回答
  •  难免孤独
    2020-12-28 20:00

    • Maintain a min heap of size 'k' in the centralized server.
    • Initially insert first k elements into the min heap.
    • For the remaining elements
      • Check(peek) for the min element in the heap (O(1))
      • If the min element is lesser than the current element, then remove the min element from heap and insert the current element.
    • Finally min heap will have 'k' largest elements
    • This would require n(log k) time.

提交回复
热议问题