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 Java code after sorting the array using insertion sort?
DSA Java
public class Main { public static void insertionSort(int[] arr) { 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; } } public static void main(String[] args) { int[] data = {5, 2, 9, 1}; insertionSort(data); for (int num : data) { System.out.print(num + " "); } } }
Attempts:
2 left
💡 Hint
Insertion sort places smaller elements before larger ones by shifting elements.
✗ Incorrect
The insertion sort algorithm sorts the array in ascending order by inserting each element into its correct position. The output is the sorted array: 1 2 5 9.
🧠 Conceptual
intermediateNumber of comparisons in insertion sort
For an array of size 5 sorted in descending order, how many comparisons does insertion sort perform in the worst case?
Attempts:
2 left
💡 Hint
Worst case comparisons for insertion sort is sum of first n-1 integers.
✗ Incorrect
Insertion sort in worst case compares each element with all previous elements. For size 5, comparisons = 1+2+3+4 = 10. But since each comparison is counted, total is 10 comparisons.
🔧 Debug
advancedIdentify the error in insertion sort implementation
What error will this Java code produce when running insertion sort?
DSA Java
public class Main { public static void insertionSort(int[] arr) { 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 for array index limits.
✗ Incorrect
The loop uses i <= arr.length which tries to access arr[arr.length], causing ArrayIndexOutOfBoundsException.
🚀 Application
advancedInsertion sort on linked list
Which statement is true about applying insertion sort on a singly linked list?
Attempts:
2 left
💡 Hint
Think about how nodes are moved in linked lists.
✗ Incorrect
Insertion sort on linked lists requires changing the next pointers to reorder nodes, not just swapping values.
❓ Predict Output
expertOutput after partial insertion sort steps
Given the code below, what is the state of the array after the second iteration of the outer loop?
DSA Java
public class Main { public static void insertionSort(int[] arr) { 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; if (i == 2) { for (int k = 0; k < arr.length; k++) { System.out.print(arr[k] + " "); } } } } public static void main(String[] args) { int[] data = {4, 3, 2, 1}; insertionSort(data); } }
Attempts:
2 left
💡 Hint
Trace the first two insertions carefully.
✗ Incorrect
After first iteration, array is {3,4,2,1}. After second iteration, 2 is inserted before 3, resulting in {2,3,4,1}.
