Challenge - 5 Problems
Insertion Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Insertion Sort on a small list
What is the output of the following insertion sort code when sorting the list [4, 3, 2, 1]?
DSA Python
def insertion_sort(arr): for i in range(1, len(arr)): key = arr[i] j = i - 1 while j >= 0 and arr[j] > key: arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key arr = [4, 3, 2, 1] insertion_sort(arr) print(arr)
Attempts:
2 left
💡 Hint
Insertion sort moves smaller elements to the front step by step.
✗ Incorrect
Insertion sort compares each element with those before it and inserts it in the correct position, resulting in a sorted list.
🧠 Conceptual
intermediateNumber of comparisons in insertion sort best case
In the best case, when the input list is already sorted, how many comparisons does insertion sort make for a list of size n?
Attempts:
2 left
💡 Hint
Best case means no shifting is needed.
✗ Incorrect
In the best case, each element is compared once with the previous element, so total comparisons are n - 1.
❓ Predict Output
advancedOutput after partial insertion sort steps
What is the state of the list after the second iteration (i=2) of insertion sort on [5, 3, 4, 1]?
DSA Python
def insertion_sort_partial(arr): for i in range(1, len(arr)): key = arr[i] j = i - 1 while j >= 0 and arr[j] > key: arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key if i == 2: print(arr) break arr = [5, 3, 4, 1] insertion_sort_partial(arr)
Attempts:
2 left
💡 Hint
Insertion sort sorts elements up to the current index.
✗ Incorrect
After sorting first three elements, the list becomes [3, 4, 5, 1].
🔧 Debug
advancedIdentify the error in insertion sort code
What error will this insertion sort code cause when run on [2, 1]?
Code:
for i in range(1, len(arr)):
key = arr[i]
j = i
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
DSA Python
arr = [2, 1] for i in range(1, len(arr)): key = arr[i] j = i while j >= 0 and arr[j] > key: arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key print(arr)
Attempts:
2 left
💡 Hint
Check the starting value of j and array indexing.
✗ Incorrect
j is initialized to i instead of i-1. The while condition arr[j] > key is arr[i] > arr[i], which is false, so the loop is not entered. Then arr[j + 1] = key accesses arr[i + 1], which is out of range, causing IndexError.
❓ Predict Output
expertInsertion sort on linked list output
What is the output of the following code for insertion sort on the linked list 4 -> 2 -> 3 -> 1 -> null?
DSA Python
class Node: def __init__(self, val): self.val = val self.next = None def insertion_sort_linked_list(head): if not head or not head.next: return head sorted_head = head current = head.next sorted_head.next = None while current: temp = current.next if current.val < sorted_head.val: current.next = sorted_head sorted_head = current else: search = sorted_head while search.next and search.next.val < current.val: search = search.next current.next = search.next search.next = current current = temp return sorted_head # Create linked list 4 -> 2 -> 3 -> 1 -> null head = Node(4) head.next = Node(2) head.next.next = Node(3) head.next.next.next = Node(1) sorted_head = insertion_sort_linked_list(head) # Print sorted linked list result = [] current = sorted_head while current: result.append(current.val) current = current.next print(result)
Attempts:
2 left
💡 Hint
Insertion sort on linked list inserts nodes in sorted order one by one.
✗ Incorrect
The insertion sort rearranges nodes to ascending order: 1 -> 2 -> 3 -> 4 -> null.
