Challenge - 5 Problems
Sorting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Merge Sort on an Unsorted Array
What is the output of the following C code after sorting the array using merge sort?
DSA C
#include <stdio.h> void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[n1], R[n2]; for (int i = 0; i < n1; i++) L[i] = arr[l + i]; for (int j = 0; j < n2; j++) R[j] = arr[m + 1 + j]; int i = 0, j = 0, k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } void mergeSort(int arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; mergeSort(arr, l, m); mergeSort(arr, m + 1, r); merge(arr, l, m, r); } } int main() { int arr[] = {5, 2, 9, 1, 5, 6}; int n = sizeof(arr) / sizeof(arr[0]); mergeSort(arr, 0, n - 1); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Think about how merge sort divides and merges the array.
✗ Incorrect
Merge sort divides the array into halves recursively and merges them in sorted order. The final output is the sorted array in ascending order.
🧠 Conceptual
intermediateWhy is Sorting Important for Binary Search?
Why must an array be sorted before applying binary search?
Attempts:
2 left
💡 Hint
Think about how binary search decides which half to search next.
✗ Incorrect
Binary search relies on the order of elements to decide whether to search the left or right half. Without sorting, this logic fails.
❓ Predict Output
advancedOutput of Quick Sort Partition Step
What is the state of the array after the partition function completes in quick sort for the given input?
DSA C
#include <stdio.h> int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] < pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; } int main() { int arr[] = {10, 80, 30, 90, 40, 50, 70}; int n = sizeof(arr) / sizeof(arr[0]); int pi = partition(arr, 0, n - 1); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
The pivot is the last element; elements less than pivot go left.
✗ Incorrect
Partition moves elements less than pivot to the left and places pivot in correct position. The array after partition is: 10 30 40 50 70 90 80 with pivot index 4.
🔧 Debug
advancedIdentify the Bug in Bubble Sort Implementation
What is the output of this bubble sort code and what is the bug causing it?
DSA C
#include <stdio.h> void bubbleSort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } int main() { int arr[] = {3, 2, 1}; int n = sizeof(arr) / sizeof(arr[0]); bubbleSort(arr, n); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
💡 Hint
Check the inner loop boundary conditions carefully.
✗ Incorrect
The inner loop should run till j < n - i - 1 to avoid out-of-bounds access. The corrected code sorts the array correctly.
🚀 Application
expertUsing Sorting to Find the Intersection of Two Arrays
Given two unsorted arrays, which approach correctly finds their intersection using sorting?
Attempts:
2 left
💡 Hint
Sorting both arrays allows linear time intersection with two pointers.
✗ Incorrect
Sorting both arrays lets you scan them simultaneously with two pointers to find common elements efficiently.
