Which STL sequence container provides constant-time random access AND amortized constant-time push_back, but only linear-time insertion in the middle?
Cpp Cpp Sequence Containers
Question · Easy
Given std::vector<int> v = {1,2,3,4,5}; v.erase(v.begin() + 2); what does v contain afterwards?
What is the iterator category of std::list<T>::iterator?
Cpp Cpp Iterators
Tech Exam Prep Podcast
Listen on Spotify
Podcast
Why does std::sort require its iterator range to be random-access?
Cpp Cpp Algorithms
Question · Medium
What does this code print? #include <iostream> #include <vector> int main() { std::vector<int> v; v.reserve(10); std::cout << v.size() << ' ' << v.capacity(); }
What is the time complexity of std::map<Key, Value>::find?
Cpp Cpp Associative Containers
Which function object should you pass as the third template argument of std::priority_queue<int> to turn it into a min-heap?
Cpp Cpp Container Adapters
What does this code print? #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6}; std::sort(v.begin(), v.end()); for (int x : v) std::cout << x; }
What does the erase-remove idiom achieve?
What does this code print? #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = {1, 2, 3, 2, 4, 2, 5}; auto it = std::remove(v.begin(), v.end(), 2); std::cout << v.size() << ' ' << (it - v.begin()); }
What does this code print? #include <iostream> #include <set> int main() { std::set<int> s = {5, 3, 8, 1, 5, 3}; for (int x : s) std::cout << x << ' '; }
What does this code print? #include <iostream> #include <map> int main() { std::map<std::string, int> m; m["a"] = 1; std::cout << m["b"]; }
How does std::map::at differ from std::map::operator[]?
What does this code print? #include <iostream> #include <vector> #include <numeric> int main() { std::vector<int> v = {1, 2, 3, 4, 5}; int sum = std::accumulate(v.begin(), v.end(), 10); std::cout << sum; }
Cpp Cpp Numeric Algorithms
What does this code print? #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = {1, 2, 3, 4, 5}; auto it = std::find(v.begin(), v.end(), 3); std::cout << *it << ' ' << (it - v.begin()); }
Which header declares std::sort, std::find, std::transform, and std::copy?
What does this code print? #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = {1, 2, 3, 4}; std::vector<int> w(v.size()); std::transform(v.begin(), v.end(), w.begin(), [](int x){ return x * x; }); for (int x : w) std::cout << x << ' '; }
What is the time complexity of inserting at the front of a std::deque?
Why must the comparator passed to std::sort or std::set implement strict weak ordering?
Cpp Cpp Functors
Question · Hard
What does this code print? #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = {1, 3, 5, 7, 9}; auto it = std::lower_bound(v.begin(), v.end(), 5); std::cout << (it - v.begin()) << ' ' << *it; }
Cpp Cpp Binary Search
What does std::upper_bound return for value 5 in a sorted vector {1, 3, 5, 5, 7}?
What does this code print? #include <iostream> #include <iomanip> int main() { std::cout << std::setw(5) << 42 << '|' << 7; }
Cpp Cpp Streams
What does this code print? #include <iostream> #include <iomanip> int main() { std::cout << std::fixed << std::setprecision(2) << 3.14159; }
What does this code print? #include <iostream> int main() { std::cout << std::boolalpha << (1 == 1) << ' ' << !true; }
Given a function template template <typename T> T add(T a, T b) { return a + b; } what happens at the call add(1, 2.0);?
Cpp Cpp Templates
What does this code print? #include <iostream> template <typename T> T max_of(T a, T b) { return a > b ? a : b; } template <> const char* max_of<const char*>(const char* a, const char* b) { return std::string(a) > std::string(b) ? a : b; } int main() { std::cout << max_of("apple", "banana"); }
Which is an example of PARTIAL specialization (legal in C++)?
What does this code print? #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = {1, 2, 3, 4, 5}; int n = std::count_if(v.begin(), v.end(), [](int x){ return x % 2 == 0; }); std::cout << n; }
Which iterator category is required by std::find?
What does this code print? #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = {1, 2, 3, 4, 5, 6}; auto it = std::partition(v.begin(), v.end(), [](int x){ return x % 2 == 0; }); std::cout << (it - v.begin()); }
Which expression correctly creates a min-heap from std::vector<int> v?
Cpp Cpp Heap
What does std::stable_sort guarantee that std::sort does not?
What does this code print? #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6}; std::nth_element(v.begin(), v.begin() + 3, v.end()); std::cout << v[3]; }
What does this code print? #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = {1, 2, 3}; std::vector<int> w = {4, 5, 6}; std::vector<int> r(6); std::merge(v.begin(), v.end(), w.begin(), w.end(), r.begin()); for (int x : r) std::cout << x; }
Which header must you include to use std::function and std::bind?
What does this code print? #include <iostream> #include <functional> int sub(int a, int b) { return a - b; } int main() { auto f = std::bind(sub, std::placeholders::_2, std::placeholders::_1); std::cout << f(5, 2); }
What is the difference between std::ref and std::cref?
What does this code print? #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = {1, 2, 3, 4, 5}; std::reverse(v.begin(), v.end()); for (int x : v) std::cout << x; }
What does this code print? #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = {1, 2, 3, 4, 5}; std::rotate(v.begin(), v.begin() + 2, v.end()); for (int x : v) std::cout << x; }
What is wrong with std::vector<bool>?
Which container does NOT invalidate iterators when an unrelated element is inserted?
What does this code print? #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = {1, 1, 2, 2, 3, 3, 3, 4}; auto it = std::unique(v.begin(), v.end()); v.erase(it, v.end()); for (int x : v) std::cout << x; }
What is the result of std::accumulate(v.begin(), v.end(), std::string{}, [](std::string s, int x){ return s + std::to_string(x); }) for v = {1, 2, 3}?
What does this code print? #include <iostream> #include <map> int main() { std::map<int, std::string> m = {{2, "b"}, {1, "a"}, {3, "c"}}; for (auto& [k, v] : m) std::cout << k << v; }
What is the time complexity of std::multiset<int>::erase(value)?
What does this code print? #include <iostream> #include <vector> #include <algorithm> #include <iterator> int main() { std::vector<int> v = {1, 2, 3, 4, 5}; std::copy_if(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "), [](int x){ return x > 2; }); }
What does std::back_inserter return?
Loading more…