Data Structures & AlgorithmsHardProduct company OA · Infosys pattern

k sorted lists contain N elements in total. They are merged into one sorted list by repeatedly taking the smallest head from a min-heap that holds one element per list. What is the time complexity?

  1. A. O(N log N)
  2. B. O(N k)
  3. C. O(k log N)
  4. D. O(N log k)

Correct answer: D. O(N log k)

The heap never holds more than k entries, so each of the N extract-min + insert pairs costs O(log k): O(N log k) overall. Concatenating and sorting would be O(N log N); scanning all k heads for each output element is O(N k).

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic