AlgoMaster Logo

STL Algorithms - Quiz

Last Updated: December 6, 2025

1 min read

STL Algorithms Quiz

31 quizzes

1
Multiple Choice

Which header primarily contains numeric algorithms like std::accumulate and std::inner_product?

2
Multiple Choice

Which STL algorithm is most appropriate to check if a value exists in a std::vector without modifying it?

3
Multiple Choice

What is a key difference between std::sort and std::stable_sort?

4
Multiple Choice

Which algorithm is best suited to sort only the smallest k elements of a large vector?

5
Multiple Choice

Which requirement must hold before using std::binary_search on a std::vector<int>?

6
Multiple Choice

Which algorithm is most appropriate to apply a lambda to each element without changing the container size?

7
Multiple Choice

Which modifying algorithm is best suited to copy elements from one container to another with a transformation applied?

8
Multiple Choice

Which algorithm counts elements satisfying a predicate without modifying the container?

9
Multiple Choice

You need to compute the sum of values in a std::vector<double>. Which is the most idiomatic STL algorithm?

10
Multiple Choice

Which algorithm would you use to remove all zeros from a std::vector<int> v while minimizing reallocations?

11
Multiple Choice

For parallel numeric reduction on a large vector using C++17, which algorithm is appropriate?

12
Code Completion

Use std::sort to sort a vector of ints in ascending order

cpp
1
std::(v.begin(), v.end());

Click an option to fill the blank:

13
Code Completion

Use binary_search to check if id exists in sortedIds

cpp
1
bool found = std::(sortedIds.begin(), sortedIds.end(), id);

Click an option to fill the blank:

14
Code Completion

Accumulate the total size of all strings in a vector<std::string>

cpp
1
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:

15
Output Prediction

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}
16
Output Prediction

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}
17
Output Prediction

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}
18
Output Prediction

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}
19
Output Prediction

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}
20
Bug Spotting

Find the bug related to algorithm misuse and lifetime

Click on the line(s) that contain the bug.

cpp
1
#include <vector>
2
#include <algorithm>
3
 
4
std::vector<int>::iterator findValue() {
5
    std::vector<int> data{1,2,3,4};
6
    auto it = std::find(data.begin(), data.end(), 3);
7
    return it;
8
}
21
Bug Spotting

Find the bug related to sorting with an inconsistent comparator

Click on the line(s) that contain the bug.

cpp
1
#include <vector>
2
#include <algorithm>
3
 
4
struct Item {
5
    int id;
6
    int priority;
7
};
8
 
9
int main() {
10
    std::vector<Item> items{{1,2},{2,1},{3,3}};
11
    std::sort(items.begin(), items.end(), [](const Item& a, const Item& b){
12
        if (a.priority == b.priority) return a.id < b.id;
13
        return a.priority <= b.priority; // comparator
14
    });
15
    return 0;
16
}
22
Hotspot Selection

Click the line that can cause undefined behavior when using std::copy

Click on the line to select.

cpp
1
#include <algorithm>
2
#include <vector>
3
 
4
int main() {
5
    std::vector<int> v{1,2,3,4,5};
6
    std::copy(v.begin(), v.end(), v.begin() + 1);
7
    return 0;
8
}
23
Hotspot Selection

Click the line that incorrectly assumes the vector is sorted before binary_search

Click on the line to select.

cpp
1
#include <vector>
2
#include <algorithm>
3
#include <iostream>
4
 
5
int main() {
6
    std::vector<int> v{3,1,4,2};
7
    if (std::binary_search(v.begin(), v.end(), 3)) {
8
        std::cout << "found\n";
9
    }
10
    return 0;
11
}
24
Fill in the Blanks

Complete the code to find and print the first even number using std::find_if

cpp
1
#include <algorithm>
2
#include <vector>
3
#include <iostream>
4
5
int main() {
6
std::vector<int> v{1,3,5,6,7};
7
auto it = std::find_if(v.(), v.(), [](int x){ return x % 2 == 0; });
8
if (it != v.end()) {
9
std::cout << *it << "\n";
10
}
11
}

Click an option to fill blank 1:

25
Fill in the Blanks

Fill in the blanks to use std::sort with a custom comparator for descending order

cpp
1
#include <algorithm>
2
#include <vector>
3
4
int main() {
5
std::vector<int> v{3,1,4,2};
6
std::sort(v.(), v.(), [](int a, int b){ return ; });
7
}

Click an option to fill blank 1:

26
Fill in the Blanks

Complete the code to compute prefix sums with std::partial_sum

cpp
1
#include <numeric>
2
#include <vector>
3
4
int main() {
5
std::vector<int> v{1,2,3};
6
std::vector<int> prefix(v.size());
7
std::partial_sum(v.(), v.(), prefix.());
8
}

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the erase-remove idiom to remove zeros from the vector

cpp
1
#include <algorithm>
2
#include <vector>
3
4
int main() {
5
std::vector<int> v{0,1,0,2,3};
6
v.erase(std::(v.(), v.(), 0), v.());
7
}

Click an option to fill blank 1:

28
Matching

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.

29
Matching

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.

30
Sequencing

Order the steps to use std::binary_search correctly on a vector

Drag and drop to reorder, or use the arrows.

31
Sequencing

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.

Premium Content

Subscribe to unlock full access to this content and more premium articles.