Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to perform a stable sort using std::stable_sort.
DSA C++
#include <vector> #include <algorithm> std::vector<int> data = {4, 2, 5, 2, 3}; std::stable_sort(data.begin(), data.[1]());
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using begin() twice instead of begin() and end()
Using reverse iterators which sort in reverse order
✗ Incorrect
std::stable_sort requires the range from begin() to end() to sort the entire vector stably.
2fill in blank
mediumComplete the code to check if a sorting algorithm is stable by comparing original and sorted vectors.
DSA C++
#include <vector> #include <algorithm> #include <iostream> struct Item { int key; char value; }; std::vector<Item> items = {{1, 'a'}, {2, 'b'}, {1, 'c'}}; std::stable_sort(items.begin(), items.end(), [](const Item &a, const Item &b) { return a.key < b.key; }); for (const auto& item : items) { std::cout << item.key[1]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Printing keys without spaces causing output to be hard to read
Using newline which prints each key on a new line
✗ Incorrect
Printing keys separated by spaces helps visualize the order and check stability.
3fill in blank
hardFix the error in the code that attempts to use std::sort to maintain stability.
DSA C++
#include <vector> #include <algorithm> struct Record { int id; int score; }; std::vector<Record> records = {{1, 90}, {2, 90}, {3, 80}}; std::sort(records.begin(), records.end(), [](const Record &a, const Record &b) { return a.score [1] b.score; });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' or '>=' in comparator causing runtime errors
Expecting std::sort to be stable
✗ Incorrect
std::sort is not stable regardless of comparator, but the comparator must use strict weak ordering with '<' to avoid errors.
4fill in blank
hardFill both blanks to create a lambda comparator that sorts by score descending and then by id ascending.
DSA C++
auto cmp = [](const Record &a, const Record &b) {
if (a.score [1] b.score) return true;
if (a.score == b.score) return a.id [2] b.id;
return false;
}; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' for score causing ascending order instead of descending
Using '>' for id causing descending order instead of ascending
✗ Incorrect
To sort descending by score, use '>' and for ascending by id, use '<'.
5fill in blank
hardFill all three blanks to create a stable sort call that sorts a vector of pairs by first element ascending and second element descending.
DSA C++
std::stable_sort(pairs.begin(), pairs.end(), [](const auto &a, const auto &b) {
if (a.first [1] b.first) return true;
if (a.first [3] b.first) return a.second [2] b.second;
return false;
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' for first element causing descending order
Using '<' for second element causing ascending order
Using '!=' instead of '==' for equality check
✗ Incorrect
Sort ascending by first element using '<', descending by second element using '>', and check equality with '=='.