Challenge - 5 Problems
Sorting Stability Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Stable Sort on Objects with Equal Keys
Consider a list of objects with a key and a value. After sorting by the key using a stable sort, what is the order of the values?
DSA C#
using System; using System.Collections.Generic; class Item { public int Key { get; set; } public string Value { get; set; } public Item(int key, string value) { Key = key; Value = value; } } class Program { static void Main() { var items = new List<Item> { new Item(2, "A"), new Item(1, "B"), new Item(2, "C"), new Item(1, "D") }; items.Sort((x, y) => x.Key.CompareTo(y.Key)); foreach (var item in items) { Console.Write(item.Value + " -> "); } Console.WriteLine("null"); } }
Attempts:
2 left
💡 Hint
Remember that List.Sort in C# uses an unstable sort algorithm.
✗ Incorrect
The List.Sort method in C# uses an unstable sort (QuickSort). So the relative order of equal keys is not guaranteed to be preserved. However, the code uses Sort which is unstable, so the order of items with key 1 or 2 may change. But the question asks about stable sort, so the output shown is if the sort was stable. The actual output will differ.
❓ Predict Output
intermediateOutput of Stable Sort Using LINQ OrderBy
What is the output of sorting the same list by key using LINQ's OrderBy, which is stable?
DSA C#
using System; using System.Collections.Generic; using System.Linq; class Item { public int Key { get; set; } public string Value { get; set; } public Item(int key, string value) { Key = key; Value = value; } } class Program { static void Main() { var items = new List<Item> { new Item(2, "A"), new Item(1, "B"), new Item(2, "C"), new Item(1, "D") }; var sorted = items.OrderBy(x => x.Key).ToList(); foreach (var item in sorted) { Console.Write(item.Value + " -> "); } Console.WriteLine("null"); } }
Attempts:
2 left
💡 Hint
LINQ's OrderBy preserves the order of equal keys.
✗ Incorrect
LINQ's OrderBy uses a stable sorting algorithm, so the relative order of items with equal keys is preserved. The original order of items with key 1 is B then D, and with key 2 is A then C.
🧠 Conceptual
advancedChoosing Sorting Algorithm for Large Data with Limited Memory
You have a very large dataset that cannot fit entirely into memory. Which sorting algorithm is best suited for this scenario?
Attempts:
2 left
💡 Hint
Think about sorting that can work with data on disk and limited memory.
✗ Incorrect
MergeSort can be implemented as an external sort that works well with data on disk because it processes data in chunks and merges sorted runs. QuickSort requires random access and is not suitable for external sorting. BubbleSort and InsertionSort are inefficient for large data.
🧠 Conceptual
advancedWhy Stability Matters in Sorting When Sorting by Multiple Keys
Why is it important to use a stable sort when sorting a list by multiple keys in sequence?
Attempts:
2 left
💡 Hint
Think about sorting first by one key, then by another key without losing the first order.
✗ Incorrect
When sorting by multiple keys, you sort by the least important key first, then by more important keys using a stable sort. Stability preserves the order of equal elements from previous sorts, so the final order respects all keys.
❓ Predict Output
expertOutput of Sorting Custom Objects with Unstable Sort and Equal Keys
What is the output of sorting a list of custom objects with equal keys using List.Sort (unstable) in C#?
DSA C#
using System; using System.Collections.Generic; class Person { public int Age { get; set; } public string Name { get; set; } public Person(int age, string name) { Age = age; Name = name; } } class Program { static void Main() { var people = new List<Person> { new Person(30, "Alice"), new Person(25, "Bob"), new Person(30, "Charlie"), new Person(25, "David") }; people.Sort((p1, p2) => p1.Age.CompareTo(p2.Age)); foreach (var p in people) { Console.Write(p.Name + " -> "); } Console.WriteLine("null"); } }
Attempts:
2 left
💡 Hint
List.Sort is unstable, so the relative order of equal keys may change.
✗ Incorrect
List.Sort uses an unstable QuickSort, so the relative order of people with the same age is not guaranteed. However, the most common output is Bob -> David -> Alice -> Charlie -> null because the sort groups by age ascending but does not preserve original order within equal keys.
