How to use the C++ Practice Problems
- 1. Pick a problemFilter by level and choose a problem. Its task and sample tests appear below.
- 2. Write and run it locallyWrite your C++ solution here (it's saved in this browser), then run it with g++ -std=c++17 on each sample input.
- 3. Check your outputPaste what your program printed for each test and press Check. Solved problems get a tick.
Topics covered
- Containers: sort and unique, word counts with
map, two-sum withunordered_map, distinct counts in a sliding window. - Adapters: balanced brackets with
stack, k-th largest with a min-heappriority_queue. - Algorithms:
merge,rotate,transform, prefix sums and BFS on a grid. - OOP: a Rectangle class with const member functions, and shapes with an abstract base class and
unique_ptr.
All 15 reference solutions compile with g++ in C++17 mode with warnings treated as errors, and pass their tests.
Worked example: k-th largest element
Input 6 2 and 3 2 1 5 6 4 should print 5, the second largest. The duplicate case 3 2 and 5 5 4 also prints 5, because duplicates count. The suggested solution keeps a min-heap of size k: push each number and pop when the heap grows past k. The heap's top is the answer, in O(n log k) time, which is faster than sorting when k is small.
Checking your work
Run your program on each sample input and paste the output into the matching box. The checker compares line by line, ignoring spaces at line ends. Use the hint first and the solution only after a real attempt; reading a solution you couldn't have written is when the learning happens, so compare it with yours line by line.
Frequently asked questions
Can I use bits/stdc++.h?
Yes for practice with g++. In production code include only the headers you need.
Which standard do the solutions need?
C++17. One solution uses structured bindings, which need C++17.
Does the page compile my code?
No. Compile it locally; the page checks the output you paste.
Is the C++ practice problems free?
Yes. Everything in the C++ practice problems works without signing up. The first time you ask for a result, we ask once for your name and WhatsApp number; after that the C++ practice problems and every other Uncodemy tool open straight away.