Challenge - 5 Problems
Sorting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Merging Two Sorted Arrays
What is the output of the following Java code that merges two sorted arrays into one sorted array?
DSA Java
import java.util.Arrays; public class MergeSorted { public static int[] merge(int[] a, int[] b) { int[] result = new int[a.length + b.length]; int i = 0, j = 0, k = 0; while (i < a.length && j < b.length) { if (a[i] < b[j]) { result[k++] = a[i++]; } else { result[k++] = b[j++]; } } while (i < a.length) { result[k++] = a[i++]; } while (j < b.length) { result[k++] = b[j++]; } return result; } public static void main(String[] args) { int[] arr1 = {1, 3, 5}; int[] arr2 = {2, 4, 6}; int[] merged = merge(arr1, arr2); System.out.println(Arrays.toString(merged)); } }
Attempts:
2 left
💡 Hint
Think about how two sorted arrays combine while keeping order.
✗ Incorrect
The merge function compares elements from both arrays and inserts the smaller one first, resulting in a fully sorted merged array.
🧠 Conceptual
intermediateWhy Sorting is 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 which half to discard, so the array must be sorted beforehand.
🔧 Debug
advancedFind the Bug in This Sorting Code
What error will this Java code produce when sorting an array using bubble sort?
DSA Java
public class BubbleSort { public static void bubbleSort(int[] arr) { for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } public static void main(String[] args) { int[] data = {5, 3, 8, 4, 2}; bubbleSort(data); for (int num : data) { System.out.print(num + " "); } } }
Attempts:
2 left
💡 Hint
Check the inner loop boundary conditions carefully.
✗ Incorrect
The inner loop runs until j < arr.length - i, but accessing arr[j + 1] causes an index out of bounds when j reaches arr.length - i - 1.
🚀 Application
advancedUsing Sorting to Find the Kth Smallest Element
Given an unsorted array, which approach correctly finds the 3rd smallest element by sorting?
DSA Java
int[] arr = {7, 10, 4, 3, 20, 15}; // Find 3rd smallest element
Attempts:
2 left
💡 Hint
Remember array indices start at 0.
✗ Incorrect
After sorting, the 3rd smallest element is at index 2 because indexing starts from 0.
❓ Predict Output
expertOutput of Sorting Stability Test
What is the output of this Java code that sorts an array of objects by age, preserving insertion order for equal ages?
DSA Java
import java.util.*; class Person { String name; int age; Person(String n, int a) { name = n; age = a; } public String toString() { return name + ":" + age; } } public class StableSortTest { public static void main(String[] args) { List<Person> people = new ArrayList<>(); people.add(new Person("Alice", 30)); people.add(new Person("Bob", 25)); people.add(new Person("Charlie", 30)); people.add(new Person("David", 25)); people.sort(Comparator.comparingInt(p -> p.age)); for (Person p : people) { System.out.print(p + " "); } } }
Attempts:
2 left
💡 Hint
Think about what stable sorting means for equal keys.
✗ Incorrect
Java's List.sort uses a stable sort, so elements with equal age keep their original order: Bob before David, Alice before Charlie.
