Challenge - 5 Problems
Selection Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Selection Sort on a small array
What is the output of the following C++ code after performing selection sort on the array?
DSA C++
int arr[] = {64, 25, 12, 22, 11}; int n = 5; for (int i = 0; i < n - 1; i++) { int min_idx = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[min_idx]) { min_idx = j; } } int temp = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = temp; } for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Selection sort finds the smallest element and swaps it with the first unsorted element.
✗ Incorrect
Selection sort repeatedly selects the smallest element from the unsorted part and swaps it with the first unsorted element, resulting in a sorted array.
🧠 Conceptual
intermediateNumber of swaps in Selection Sort
How many swaps does the selection sort algorithm perform on an array of size n in the worst case?
Attempts:
2 left
💡 Hint
Selection sort swaps only once per outer loop iteration.
✗ Incorrect
Selection sort performs exactly one swap per iteration of the outer loop, resulting in n - 1 swaps in total.
🔧 Debug
advancedIdentify the bug in this selection sort code
What error will occur when running this selection sort code snippet?
DSA C++
int arr[] = {3, 1, 4, 2}; int n = 4; for (int i = 0; i < n; i++) { int min_idx = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[min_idx]) { min_idx = j; } } int temp = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = temp; }
Attempts:
2 left
💡 Hint
Check the inner loop boundary condition carefully.
✗ Incorrect
The inner loop uses j <= n which accesses arr[n], an invalid index causing runtime error.
🚀 Application
advancedSelection Sort on Linked List
Which of the following statements about applying selection sort on a singly linked list is true?
Attempts:
2 left
💡 Hint
Think about how swapping values differs from swapping nodes.
✗ Incorrect
Selection sort can be done on linked lists by swapping the data inside nodes, avoiding complex pointer changes.
❓ Predict Output
expertOutput after partial selection sort iterations
Given the array and partial selection sort code below, what is the array state after the second iteration of the outer loop?
DSA C++
int arr[] = {29, 10, 14, 37, 13}; int n = 5; for (int i = 0; i < 2; i++) { int min_idx = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[min_idx]) { min_idx = j; } } int temp = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = temp; } // Print array for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; }
Attempts:
2 left
💡 Hint
Trace the first two passes of selection sort carefully.
✗ Incorrect
After first iteration, smallest element 10 is at index 0; after second, next smallest 13 is at index 1.
