Challenge - 5 Problems
Insertion Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Insertion Sort on a small array
What is the output of the following C# code after sorting the array using insertion sort?
DSA C#
int[] arr = {5, 3, 8, 6, 2}; for (int i = 1; i < arr.Length; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } Console.WriteLine(string.Join(", ", arr));
Attempts:
2 left
💡 Hint
Insertion sort places each element in its correct position by shifting larger elements to the right.
✗ Incorrect
Insertion sort iterates from the second element, placing it in the correct position among the sorted elements to its left. After processing all elements, the array is sorted in ascending order.
🧠 Conceptual
intermediateInsertion Sort Best Case Time Complexity
What is the best case time complexity of insertion sort and when does it occur?
Attempts:
2 left
💡 Hint
Think about how many shifts happen if the array is already sorted.
✗ Incorrect
In the best case, insertion sort only compares each element once because all elements are already in order, resulting in O(n) time complexity.
🔧 Debug
advancedIdentify the bug in this insertion sort code
What error will occur when running this insertion sort code snippet?
DSA C#
int[] arr = {4, 2, 7}; for (int i = 1; i < arr.Length; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; }
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing carefully.
✗ Incorrect
The loop uses i <= arr.Length which causes i to reach arr.Length, an invalid index, causing IndexOutOfRangeException.
🚀 Application
advancedInsertion Sort on Linked List
Which of the following statements about applying insertion sort on a singly linked list is true?
Attempts:
2 left
💡 Hint
Think about how nodes can be moved by changing links.
✗ Incorrect
Insertion sort on linked lists can be done by changing pointers to insert nodes in the correct position, without extra arrays or space.
❓ Predict Output
expertOutput after partial insertion sort steps
Given the array and partial insertion sort steps below, what is the array state after the second iteration (i=2)?
DSA C#
int[] arr = {9, 4, 7, 3}; for (int i = 1; i <= 2; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } Console.WriteLine(string.Join(", ", arr));
Attempts:
2 left
💡 Hint
Trace each iteration carefully and watch how elements shift.
✗ Incorrect
After first iteration, array is [4,9,7,3]. After second iteration, 7 is inserted before 9, resulting in [4,7,9,3].
