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?
- A. O(N log N)
- B. O(N k)
- C. O(k log N)
- 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).
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A deque of indices gives the MINIMUM of every window of size 3 in [4, 2, 12, 3, 8, 1, 7]. What are the window minima?
- A hash table of size 10 uses linear probing. Slots 2, 3, 4, 7 and 8 are occupied and all others are empty (no deletions have happened). An…
- Quickselect with Lomuto partition (pivot = last element) looks for the 3rd smallest element of [7, 2, 9, 4, 3, 8], recursing only into the…
- Binary search finds the index of the minimum of the rotated sorted array [15, 18, 22, 3, 6, 9, 12] using: mid = (lo + hi) / 2; if a[mid] >…
- The sorted array [3, 4, 7, 10, 11] contains some positive integers. What is the 4th positive integer MISSING from it?
- For [2, 3, 0, 4], out[i] is the product of all elements except a[i], computed with prefix and suffix products (no division). What is out?