Challenge - 5 Problems
Merge Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Merge Sort on a small list
What is the output of the following merge sort code when sorting the list [4, 1, 3, 2]?
DSA Python
def merge_sort(arr): if len(arr) <= 1: return arr mid = len(arr) // 2 left = merge_sort(arr[:mid]) right = merge_sort(arr[mid:]) return merge(left, right) def merge(left, right): result = [] i = j = 0 while i < len(left) and j < len(right): if left[i] < right[j]: result.append(left[i]) i += 1 else: result.append(right[j]) j += 1 result.extend(left[i:]) result.extend(right[j:]) return result print(merge_sort([4, 1, 3, 2]))
Attempts:
2 left
💡 Hint
Think about how merge sort splits the list and merges sorted halves.
✗ Incorrect
Merge sort splits the list into halves recursively until single elements remain, then merges them back in sorted order. The final output is the sorted list [1, 2, 3, 4].
🧠 Conceptual
intermediateUnderstanding the divide step in merge sort
In merge sort, what is the main purpose of the divide step?
Attempts:
2 left
💡 Hint
Divide step breaks the problem into smaller pieces.
✗ Incorrect
The divide step splits the list into smaller parts until each part has only one element, which is the base case for merge sort.
❓ Predict Output
advancedOutput of merge function with overlapping elements
What is the output of the merge function when merging these two sorted lists: [1, 3, 5] and [2, 3, 4]?
DSA Python
def merge(left, right): result = [] i = j = 0 while i < len(left) and j < len(right): if left[i] < right[j]: result.append(left[i]) i += 1 else: result.append(right[j]) j += 1 result.extend(left[i:]) result.extend(right[j:]) return result print(merge([1, 3, 5], [2, 3, 4]))
Attempts:
2 left
💡 Hint
The merge function combines two sorted lists into one sorted list, including duplicates.
✗ Incorrect
The merge function compares elements from both lists and appends the smaller one, preserving duplicates and order, resulting in [1, 2, 3, 3, 4, 5].
🔧 Debug
advancedIdentify the error in this merge sort implementation
What error will this merge sort code produce when run on []?
DSA Python
def merge_sort(arr): if len(arr) == 1: return arr mid = len(arr) // 2 left = merge_sort(arr[:mid]) right = merge_sort(arr[mid:]) return merge(left, right) def merge(left, right): result = [] i = j = 0 while i < len(left) and j < len(right): if left[i] < right[j]: result.append(left[i]) i += 1 else: result.append(right[j]) j += 1 result.extend(left[i:]) result.extend(right[j:]) return result print(merge_sort([]))
Attempts:
2 left
💡 Hint
Check the base case condition for recursion termination.
✗ Incorrect
The base case checks if length is exactly 1, but for empty lists it recurses infinitely causing RecursionError.
🚀 Application
expertNumber of merge operations in merge sort
For a list of 8 elements, how many times is the merge function called during the entire merge sort process?
Attempts:
2 left
💡 Hint
Think about how many merges happen at each level of splitting.
✗ Incorrect
Merge sort splits the list into single elements and merges back. For 8 elements, merge is called 7 times (one less than number of elements).
