AlgoMaster Logo

STL Containers - Quiz

Last Updated: December 6, 2025

1 min read

STL Containers Quiz

31 quizzes

1
Multiple Choice

Which STL container is most appropriate for a dynamic sequence with frequent random access and frequent push_back operations?

2
Multiple Choice

Which container guarantees unique keys and keeps elements sorted by key?

3
Multiple Choice

Which container adaptor uses a deque as its default underlying container in C++?

4
Multiple Choice

You need a container of unique integers with average O(1) lookup and no ordering requirement. Which do you choose?

5
Multiple Choice

Which statement about std::list and std::forward_list is TRUE?

6
Multiple Choice

For implementing an LRU cache where you must erase elements from the middle frequently and maintain order, which container is most suitable for the list of keys?

7
Multiple Choice

Which container is BEST for implementing a task scheduler where tasks are processed by highest priority first?

8
Multiple Choice

Which is the most cache-friendly container for iterating over a large number of numeric elements repeatedly?

9
Multiple Choice

You must maintain key-value pairs with possible duplicate keys and keep them sorted by key. Which container fits?

10
Multiple Choice

Which header must you include to use std::array?

11
Multiple Choice

Which utility is best suited for returning three strongly-typed values from a function without defining a custom struct?

12
Code Completion

Choose the correct STL container to store a fixed-size collection of 10 doubles with STL-style interface.

cpp
1
std::<double, 10> samples;

Click an option to fill the blank:

13
Code Completion

Insert into an ordered set of strings

cpp
1
std::set<std::string> s; s.("alice");

Click an option to fill the blank:

14
Code Completion

Create an unordered_map from strings to ints

cpp
1
std::<std::string, int> freq;

Click an option to fill the blank:

15
Output Prediction

What is the output of this code using a deque as a double-ended queue?

1#include <iostream>
2#include <deque>
3int main() {
4    std::deque<int> dq{1, 2, 3};
5    dq.push_front(0);
6    dq.push_back(4);
7    dq.pop_front();
8    dq.pop_back();
9    for (int x : dq) std::cout << x << ' ';
10    return 0;
11}
16
Output Prediction

What does this code print when using std::list and iterator operations?

1#include <iostream>
2#include <list>
3int main() {
4    std::list<int> lst{1, 2, 3, 4};
5    auto it = lst.begin();
6    ++it; // points to 2
7    it = lst.insert(it, 10); // insert before 2, it points to 10
8    ++it; // now points to 2
9    lst.erase(it); // erase 2
10    for (int x : lst) std::cout << x << ' ';
11    return 0;
12}
17
Output Prediction

What is the output when using std::map and operator[]?

1#include <iostream>
2#include <map>
3#include <string>
4int main() {
5    std::map<std::string, int> scores;
6    scores["Alice"] = 10;
7    scores["Bob"] = 20;
8    scores["Alice"] += 5;
9    std::cout << scores.size() << '\n';
10    std::cout << scores["Alice"] << ' ' << scores["Bob"] << '\n';
11    return 0;
12}
18
Output Prediction

What is the output of this program using std::unordered_set?

1#include <iostream>
2#include <unordered_set>
3int main() {
4    std::unordered_set<int> s{1, 2, 3};
5    auto r1 = s.insert(2);
6    auto r2 = s.insert(4);
7    std::cout << s.size() << '\n';
8    std::cout << std::boolalpha << r1.second << ' ' << r2.second << '\n';
9    return 0;
10}
19
Output Prediction

What does this priority_queue example print?

1#include <iostream>
2#include <queue>
3int main() {
4    std::priority_queue<int> pq;
5    pq.push(5);
6    pq.push(1);
7    pq.push(7);
8    pq.push(3);
9    while (!pq.empty()) {
10        std::cout << pq.top() << ' ';
11        pq.pop();
12    }
13    return 0;
14}
20
Bug Spotting

Find the bug related to iterators and erasing from a std::vector

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

cpp
1
#include <vector>
2
#include <iostream>
3
int main() {
4
    std::vector<int> v{1,2,3,4,5};
5
    for (auto it = v.begin(); it != v.end(); ++it) {
6
        if (*it % 2 == 0) {
7
            v.erase(it);
8
        }
9
    }
10
    for (int x : v) std::cout << x << ' ';
11
    return 0;
12
}
21
Bug Spotting

Find the bug related to using std::array

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

cpp
1
#include <array>
2
#include <iostream>
3
void printFirst(const std::array<int, 5>& arr) {
4
    std::cout << arr[5] << '\n';
5
}
6
int main() {
7
    std::array<int, 5> a{1,2,3,4,5};
8
    printFirst(a);
9
}
10
 
22
Hotspot Selection

Click the line that causes undefined behavior due to dereferencing an invalid iterator in a list

Click on the line to select.

cpp
1
#include <list>
2
#include <iostream>
3
int main() {
4
    std::list<int> lst{1,2,3};
5
    auto it = lst.begin();
6
    it = lst.erase(it);
7
    ++it; // line to inspect
8
    std::cout << *it << '\n';
9
    return 0;
10
}
23
Hotspot Selection

Click the line that creates an unnecessary copy when pushing into a vector of std::pair

Click on the line to select.

cpp
1
#include <vector>
2
#include <utility>
3
int main() {
4
    std::vector<std::pair<int,int>> v;
5
    std::pair<int,int> p(1,2);
6
    v.push_back(p);
7
    return 0;
8
}
24
Fill in the Blanks

Fill in the blanks to iterate over a forward_list and remove all negative values correctly.

cpp
1
#include <forward_list>
2
#include <iostream>
3
int main() {
4
std::forward_list<int> fl{1, -2, 3, -4, 5};
5
auto prev = fl.();
6
auto it = fl.begin();
7
while (it != fl.end()) {
8
if (*it < 0) {
9
it = fl.(prev);
10
} else {
11
prev = it;
12
++it;
13
}
14
}
15
for (int x : fl) std::cout << x << ' ';
16
}
17

Click an option to fill blank 1:

25
Fill in the Blanks

Use std::queue to process integers in FIFO order.

cpp
1
#include <queue>
2
#include <iostream>
3
int main() {
4
std::queue<int> q;
5
q.(1);
6
q.(2);
7
q.(3);
8
while (!q.empty()) {
9
std::cout << q.() << ' ';
10
q.();
11
}
12
}
13

Click an option to fill blank 1:

26
Fill in the Blanks

Fill the blanks to use std::pair for a map-like insertion.

cpp
1
#include <map>
2
#include <string>
3
int main() {
4
std::map<std::string, int> m;
5
m.insert(std::<std::string, int>(, 42));
6
}
7

Click an option to fill blank 1:

27
Fill in the Blanks

Fill in the blanks to declare and access a std::tuple.

cpp
1
#include <tuple>
2
#include <iostream>
3
int main() {
4
std::tuple<int, std::string, double> t{, , 3.14};
5
std::cout << std::<1>(t) << '\n';
6
}
7

Click an option to fill blank 1:

28
Matching

Match each STL container with its primary characteristic.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

29
Matching

Match each associative container with its key/ordering 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 a std::map for counting word frequencies from a vector<std::string> words.

Drag and drop to reorder, or use the arrows.

31
Sequencing

Put in order the operations to safely pop all elements from a std::stack<int> s.

Drag and drop to reorder, or use the arrows.

Premium Content

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