C++ STL Algorithms and Lambdas Quiz: Sort, Accumulate, Ranges
Practise C++ STL algorithms and lambdas with 14 questions and three programs: stable sorting, nth element medians and a recursive lambda over nested lists.
- Course: C++ study plan
- Module: Algorithms and lambdas
- Kind: Checkpoint — cleared at 70%
- Reading time: 25 min
- Runtime: Clang 18 · C++20
Checkpoint — Algorithms and lambdas is the checkpoint that closes the Algorithms and lambdas module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers the whole module: the half-open range and predicate interface of <algorithm> and <numeric>, sorting with comparators and stability plus binary search on sorted ranges, transform, accumulate, remove_if and partition, lambdas as closure objects with captures, mutable and std::function, and C++20 ranges with projections and lazy views.
How it works. Fourteen questions and three programs. You need 70% on the questions and every program accepted to clear the module. You can retake it as often as you like; your best score counts.
Before you start, make sure you can answer these from memory:
- What does
std::findreturn when nothing matches, and what must you test before dereferencing it? - Why is a comparator written with
<=undefined behaviour rather than a wrong order? - When must you use
std::stable_sortinstead ofstd::sort? - What does
std::accumulate(v.begin(), v.end(), 0)compute over astd::vector<long long>? - Why does
std::remove_ifneed aneraseafter it? - What does copying a
mutablelambda copy? - Why does
std::views::iota(1) | std::views::take(5)terminate?
The three programs are a task list ordered by std::stable_sort that must keep arrival order within a priority, a median found with std::nth_element followed by k-th smallest queries, and a nested-list walker written as a recursive lambda. Read each input format before choosing the algorithm.
Common questions
What does std::accumulate(v.begin(), v.end(), 0) compute over a std::vector<long long>?
It sums in int, because the accumulator takes the type of the initial value 0, not of the elements. Large values overflow, which is undefined behaviour. Pass 0LL so the sum is a long long.
When must you use std::stable_sort instead of std::sort?
When elements with equal keys can occur and their input order matters — for example, sorting tasks by priority while keeping arrival order within a priority. std::sort leaves equal elements in an unspecified order; the alternative is a tie-break key that makes the order total.
What does copying a mutable lambda copy?
The whole closure object, including the current values of its by-value captures. The copy then changes independently, so a mutable counter passed by value to an algorithm advances only the algorithm's copy, never the caller's.
Exercises
Tasks by priority, arrival order kept
Read an integer n, then n lines of task priority (single-word task names, integer priorities). Order the tasks by priority descending with std::stable_sort, so that tasks of equal priority keep the order in which they were read — the comparator compares priority only. Print each task as <task> (<priority>).
Input: n, then n lines. Output: n lines.
5
write 2
test 3
lint 2
deploy 3
docs 1
prints
test (3)
deploy (3)
write (2)
lint (2)
docs (1)Median without sorting
Read an integer n (at least 1), then n integers, then an integer q, then q integers k (each between 1 and n). Print the median with std::nth_element — no full sort: for odd n it is the element that lands at index n / 2; for even n it is the mean of that element and the largest element before it (std::max_element over the left part, which nth_element guarantees holds nothing greater). Print it with one decimal place (std::fixed, std::setprecision(1)). Then, for each k, print the k-th smallest value using std::nth_element again.
Input: n, the values, q, the queries. Output: median=<value>, then q lines.
Example: 5 then 7 1 9 3 5 then 2 then 1 and 4 →
median=5.0
1
7Walk a nested list with a recursive lambda
The input is one nested list written as space-separated tokens: [ opens a list, ] closes it, and every other token is an integer. Example: [ 1 [ 2 3 ] [ 4 [ 5 ] ] ]. Walk it with a recursive lambda — a generic lambda that receives itself as its first parameter (auto&& self) with an explicit -> void return type, or a std::function — reading tokens with std::cin >>: an integer is added to the running sum and counted, a [ recurses one level deeper, a ] returns. Report the sum (in long long), the number of integers, and the deepest nesting, where the outermost list is depth 1.
Input: one line. Output: sum=<total>, count=<integers>, depth=<max depth>.
Example: [ 1 [ 2 3 ] [ 4 [ 5 ] ] ] →
sum=15
count=5
depth=3In this module: Algorithms and lambdas
- The algorithm library — ranges, predicates and the first dozen calls
- Sorting and searching — comparators, stability and binary search
- Transforming and reducing — transform, accumulate and the erase–remove idiom
- Lambdas in depth — captures, closures, std::function and recursion
- Ranges and views — C++20 pipelines
- Checkpoint — Algorithms and lambdas (this lesson)
← Ranges and views — C++20 pipelines · Exceptions — throw, try, catch and the standard hierarchy →