Prepin
Log in

Combine M sorted lists of varying lengths.

Approach / Explanation

Your task in this problem is to create a function that merges M sorted lists of varying lengths into a single sorted list. The function will receive a list of lists as input, where each inner list is sorted, but the outer list itself is not sorted, and the lengths of the inner lists can differ. The goal is to return one sorted list that incorporates all the elements from the sub-lists. When addressing this problem, here are some important considerations: Data Structure: We are working with a list of lists. Sorting Algorithm: Since each sub-list is already sorted, we need to devise an efficient method to merge these lists into a single sorted list rather than simply concatenating them and sorting the entire collection again. Complexity: It's also essential to evaluate the time complexity of our algorithm to ensure it remains efficient, particularly for larger inputs.

Suggested Answer

A straightforward approach to tackle this issue is to combine all elements from the sub-lists into a single main list and then sort that list. This technique is effective because it eliminates the need to create a custom sorting algorithm or to manage the varying lengths of the sub-lists. Here’s an example in Python: def merge_sorted_lists ( lists ) : result = [ ] for sublist in lists : result += sublist result . sort ( ) return result However, this solution has a time complexity of O(n log n) , where n represents the total number of elements, due to the complete sorting process performed after merging all the lists.

Alternative Answer

An improved approach involves utilizing a priority queue (min-heap) to efficiently identify the next smallest element from the sorted lists. Since the priority queue keeps the smallest element at the top, we can swiftly retrieve the smallest element from all lists without the need to check each list one by one. Below is an example in Python that employs the heapq module, which implements the heap queue algorithm (priority queue algorithm): import heapq def merge_sorted_lists ( lists ) : result = [ ] heap = [ ] # fill the heap with the first elements from each list for i in range ( len ( lists ) ) : if lists [ i ] : # ensure the sublist is not empty heapq . heappush ( heap , ( lists [ i ] [ 0 ] , i ) ) lists [ i ] = lists [ i ] [ 1 : ] # remove the first element from the original list while heap : val , index = heapq . heappop ( heap ) result . append ( val ) if lists [ index ] : # check for more elements in this sublist heapq . heappush ( heap , ( lists [ index ] [ 0 ] , index ) ) lists [ index ] = lists [ index ] [ 1 : ] return result This approach offers a more advantageous time complexity of O(n log m) , where n represents the total number of elements and m denotes the number of sub-lists, as we perform a single push and pop operation for each element in the heap.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.