Last Updated: December 6, 2025
31 quizzes
Which header primarily contains numeric algorithms like std::accumulate and std::inner_product?
Which STL algorithm is most appropriate to check if a value exists in a std::vector without modifying it?
What is a key difference between std::sort and std::stable_sort?
Which algorithm is best suited to sort only the smallest k elements of a large vector?
Which requirement must hold before using std::binary_search on a std::vector<int>?
Which algorithm is most appropriate to apply a lambda to each element without changing the container size?
Which modifying algorithm is best suited to copy elements from one container to another with a transformation applied?
Which algorithm counts elements satisfying a predicate without modifying the container?
You need to compute the sum of values in a std::vector<double>. Which is the most idiomatic STL algorithm?
Which algorithm would you use to remove all zeros from a std::vector<int> v while minimizing reallocations?
For parallel numeric reduction on a large vector using C++17, which algorithm is appropriate?
Use std::sort to sort a vector of ints in ascending order
std::(v.begin(), v.end());Click an option to fill the blank:
Use binary_search to check if id exists in sortedIds
bool found = std::(sortedIds.begin(), sortedIds.end(), id);Click an option to fill the blank:
Accumulate the total size of all strings in a vector<std::string>
auto total = std::accumulate(names.begin(), names.end(), 0u, [](unsigned s, const std::string& str){ return s + str.(); });Click an option to fill the blank:
What is the output of this code using std::count and std::count_if?
1#include <iostream>
2#include <vector>
3#include <algorithm>
4
5int main() {
6 std::vector<int> v{1, 2, 2, 3, 4, 2, 5};
7 auto c1 = std::count(v.begin(), v.end(), 2);
8 auto c2 = std::count_if(v.begin(), v.end(), [](int x){ return x % 2 == 0; });
9 std::cout << c1 << " " << c2 << "\n";
10 return 0;
11}What is the output after std::sort with custom comparator?
1#include <iostream>
2#include <vector>
3#include <algorithm>
4#include <string>
5
6struct Person {
7 std::string name;
8 int age;
9};
10
11int main() {
12 std::vector<Person> people{{"Bob", 25}, {"Alice", 25}, {"Charlie", 30}};
13 std::sort(people.begin(), people.end(), [](const Person& a, const Person& b){
14 if (a.age != b.age) return a.age < b.age;
15 return a.name < b.name;
16 });
17 for (const auto& p : people) {
18 std::cout << p.name << " ";
19 }
20 std::cout << "\n";
21 return 0;
22}What is the output when using std::partial_sort?
1#include <iostream>
2#include <vector>
3#include <algorithm>
4
5int main() {
6 std::vector<int> v{7, 3, 9, 1, 5, 8};
7 std::partial_sort(v.begin(), v.begin() + 3, v.end());
8 for (int i = 0; i < 6; ++i) std::cout << v[i] << " ";
9 std::cout << "\n";
10 return 0;
11}What does this code print using std::accumulate with a custom operation?
1#include <iostream>
2#include <vector>
3#include <numeric>
4
5int main() {
6 std::vector<int> v{1, 2, 3, 4};
7 int result = std::accumulate(v.begin(), v.end(), 1, [](int a, int b){ return a * b; });
8 std::cout << result << "\n";
9 return 0;
10}What is the output when using std::remove_if followed by erase?
1#include <iostream>
2#include <vector>
3#include <algorithm>
4
5int main() {
6 std::vector<int> v{1, 2, 3, 4, 5, 6};
7 auto it = std::remove_if(v.begin(), v.end(), [](int x){ return x % 2 == 0; });
8 v.erase(it, v.end());
9 for (int x : v) std::cout << x;
10 std::cout << "\n";
11 return 0;
12}Find the bug related to algorithm misuse and lifetime
Click on the line(s) that contain the bug.
#include <vector>#include <algorithm> std::vector<int>::iterator findValue() { std::vector<int> data{1,2,3,4}; auto it = std::find(data.begin(), data.end(), 3); return it;}Find the bug related to sorting with an inconsistent comparator
Click on the line(s) that contain the bug.
#include <vector>#include <algorithm> struct Item { int id; int priority;}; int main() { std::vector<Item> items{{1,2},{2,1},{3,3}}; std::sort(items.begin(), items.end(), [](const Item& a, const Item& b){ if (a.priority == b.priority) return a.id < b.id; return a.priority <= b.priority; // comparator }); return 0;}Click the line that can cause undefined behavior when using std::copy
Click on the line to select.
#include <algorithm>#include <vector> int main() { std::vector<int> v{1,2,3,4,5}; std::copy(v.begin(), v.end(), v.begin() + 1); return 0;}Click the line that incorrectly assumes the vector is sorted before binary_search
Click on the line to select.
#include <vector>#include <algorithm>#include <iostream> int main() { std::vector<int> v{3,1,4,2}; if (std::binary_search(v.begin(), v.end(), 3)) { std::cout << "found\n"; } return 0;}Complete the code to find and print the first even number using std::find_if
#include <algorithm>#include <vector>#include <iostream>int main() { std::vector<int> v{1,3,5,6,7}; auto it = std::find_if(v.(), v.(), [](int x){ return x % 2 == 0; }); if (it != v.end()) { std::cout << *it << "\n"; }}Click an option to fill blank 1:
Fill in the blanks to use std::sort with a custom comparator for descending order
#include <algorithm>#include <vector>int main() { std::vector<int> v{3,1,4,2}; std::sort(v.(), v.(), [](int a, int b){ return ; });}Click an option to fill blank 1:
Complete the code to compute prefix sums with std::partial_sum
#include <numeric>#include <vector>int main() { std::vector<int> v{1,2,3}; std::vector<int> prefix(v.size()); std::partial_sum(v.(), v.(), prefix.());}Click an option to fill blank 1:
Complete the erase-remove idiom to remove zeros from the vector
#include <algorithm>#include <vector>int main() { std::vector<int> v{0,1,0,2,3}; v.erase(std::(v.(), v.(), 0), v.());}Click an option to fill blank 1:
Match the STL algorithm with its primary purpose
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match the searching algorithm with its requirement or behavior
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Order the steps to use std::binary_search correctly on a vector
Drag and drop to reorder, or use the arrows.
Order the steps to remove elements < 0 using erase-remove_if on a std::vector<int>
Drag and drop to reorder, or use the arrows.