Comfortable with loops and arrays
C++ Intermediate
- 30 lessons
- About 60 minutes per lesson
Build on C++ fundamentals to choose standard containers, estimate running costs and optimise batches of queries using sorting, hashing and prefix sums.
View the syllabus
What you’ll be able to do
- Choose among sequences, maps, ordered sets and heaps based on access patterns.
- Explain the cost of scanning, sorting, binary search and prefix sums.
- Check an optimised solution against a small reference and boundary tests.
Course syllabus
Open a module for the lesson breakdown, a worked example and an assignment.
Lessons 1–5 Complexity, numeric range and debugging
Use data size to judge feasibility, then verify numeric types.
- Estimate the work of single and nested loops.
- Compare growth in O(n), O(n log n) and O(n²).
- Store large results in long long and widen an operand before multiplying.
- Track down out-of-bounds access, uninitialised values and empty-container access.
- Compare behaviour on generated small and large inputs.
Lessons 6–10 Maps, sets and counting
Choose map, unordered_map, set or vector based on query requirements.
- Count arbitrary names with unordered_map.
- Use map to report counts in key order.
- Use set for membership checks and deduplication.
- Look up missing keys with find without inserting defaults.
- Use a vector when the key range is small and contiguous.
Lessons 11–15 Custom sorting and binary-search bounds
Define a sort rule and locate ranges quickly in ordered data.
- Represent multi-field records with struct.
- Write a comparator that handles equality without using <=.
- Use lower_bound for the first position at or above the target.
- Use upper_bound for the first position strictly above the target.
- Count a closed interval with both bounds and check duplicates and misses.
Lessons 16–20 Stacks, queues and priority queues
Choose a container based on the processing order the task requires.
- Use a stack to match brackets and explain last-in-first-out.
- Use a queue to serve requests in arrival order.
- Manipulate both ends of a deque and identify when it is useful.
- Use priority_queue to keep the most urgent item available.
- Maintain a fixed number of largest elements with a min-heap.
Lessons 21–25 Prefix totals and batch queries
Precompute totals to turn repeated interval scans into two lookups.
- Build n+1 prefix sums starting at zero.
- Answer zero-indexed inclusive intervals with p[r+1] - p[l].
- Build a prefix count for one category.
- Use binary search on timestamps to find the queried index range.
- Compare with a direct scan and check first, last and empty ranges.
Lessons 26–30 Project: log queries
Combine containers, sorting and prefix sums in a batch query tool.
- Define the timestamp, name and quantity fields for a log record.
- Build a name-frequency map and sort the log by time.
- Implement time-window count and total queries.
- Write a direct-scan reference and compare every result on small data.
- Deliver the tool, boundary tests and preprocessing and query complexity.
A sample exercise
For n=200000, compare one full scan with examining every distinct index pair.
See the worked example →Common questions
What is the first lesson?
Estimate the work of single and nested loops. · Complexity, numeric range and debugging
How can I check my progress?
Submit a log-query tool for counts by name, time window and interval totals. Explain its complexity and compare it with a direct scan on small inputs.