AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started

Competitive Programming

The contest toolkit in C++: complexity budgets, prefix sums, bit tricks, binary search on the answer, greedy proofs, number theory, DSU, shortest paths, segment trees, DP optimisations and string algorithms — judged at the limits.

12 modules · 24 lessons · ~13 h · exercises run on Clang 18 · C++20

Syllabus

Nothing is locked. Every module has a PDF.

01

Fast I/O and complexity budgets

you are here
The contest template, fast input and output, long long by default, and reading the constraints to know which complexity will pass in one second.
0/2 · 60 minPDF
02

Prefix sums and difference arrays

Answer any range sum in O(1) after one O(n) pass, and apply many range updates with two writes each — the two halves of one idea.
0/2 · 60 minPDF
03

Bit manipulation

Set, clear, toggle and test bits, XOR's cancelling pairs, popcount, and every subset of n ≤ 20 items as a mask from 0 to 2ⁿ − 1.
0/2 · 60 minPDF
04

Binary search on the answer

Minimise the maximum, maximise the minimum: write a yes/no check for one guess, prove it monotone, and let about sixty halvings find the best value — with long long ranges and checks that cannot overflow.
0/2 · 59 minPDF
05

Greedy algorithms and exchange proofs

Sort, sweep, commit: activity selection by earliest finish, event sweeps for platforms and rooms, ordering by an exchange argument — and the counterexamples that send a problem to DP instead.
0/2 · 59 minPDF
06

Number theory: sieve, modular arithmetic, nCr

The Sieve of Eratosthenes with prefix counts, exponentiation by squaring, inverses by Fermat's little theorem, nCr mod 1e9+7 from factorial tables, and __int128 for moduli near 10^18.
0/2 · 60 minPDF
07

Disjoint set union (DSU)

Union-find in twelve lines: a parent array, path compression and union by size — components, cycle detection and Kruskal's minimum spanning tree at contest limits.
0/2 · 59 minPDF
08

Shortest paths: BFS, 0-1 BFS, Dijkstra

Choose the algorithm from the weights — BFS for equal edges, 0-1 BFS with a deque, Dijkstra with a priority_queue and lazy deletion — and write it right under a clock.
0/2 · 61 minPDF
09

Topological sort and DAGs

Kahn's algorithm with in-degrees, a min-heap for the smallest order, the cycle test by count, and dynamic programming in topological order: longest paths, path counts, semesters.
0/2 · 59 minPDF
10

Segment trees and Fenwick trees

Range queries with updates in O(log n): a segment tree for sums and minimums, a Fenwick tree in ten lines with i & -i, and counting inversions by value.
0/2 · 75 minPDF
11

DP optimisations

Make the transition cheap: prefix sums over the previous row, a monotonic deque for a sliding-window maximum, and bitmask DP over subsets of up to twenty items.
0/2 · 76 minPDF
12

String algorithms: KMP, Z and hashing

Linear-time matching with the KMP prefix function and the Z-function, borders and periods, and O(1) substring comparison with a double rolling hash.
0/2 · 76 minPDF

Learn Competitive Programming: Free Competitive Programming Tutorial in 24 Lessons

Learn Competitive Programming free: 24 lessons in 12 modules, from first programs to interview questions, with exercises judged on Clang 18 · C++20 and a certificate.

  • Language: Competitive Programming
  • Runtime: Clang 18 · C++20
  • Modules: 12
  • Lessons: 24
  • Reading time: about 13 hours
  • Cost: Free on every plan, with a certificate

The contest toolkit in C++: complexity budgets, prefix sums, bit tricks, binary search on the answer, greedy proofs, number theory, DSU, shortest paths, segment trees, DP optimisations and string algorithms — judged at the limits.

Syllabus

Module 1: Fast I/O and complexity budgets

The contest template, fast input and output, long long by default, and reading the constraints to know which complexity will pass in one second.

Module 2: Prefix sums and difference arrays

Answer any range sum in O(1) after one O(n) pass, and apply many range updates with two writes each — the two halves of one idea.

Module 3: Bit manipulation

Set, clear, toggle and test bits, XOR's cancelling pairs, popcount, and every subset of n ≤ 20 items as a mask from 0 to 2ⁿ − 1.

Module 4: Binary search on the answer

Minimise the maximum, maximise the minimum: write a yes/no check for one guess, prove it monotone, and let about sixty halvings find the best value — with long long ranges and checks that cannot overflow.

Module 5: Greedy algorithms and exchange proofs

Sort, sweep, commit: activity selection by earliest finish, event sweeps for platforms and rooms, ordering by an exchange argument — and the counterexamples that send a problem to DP instead.

Module 6: Number theory: sieve, modular arithmetic, nCr

The Sieve of Eratosthenes with prefix counts, exponentiation by squaring, inverses by Fermat's little theorem, nCr mod 1e9+7 from factorial tables, and __int128 for moduli near 10^18.

Module 7: Disjoint set union (DSU)

Union-find in twelve lines: a parent array, path compression and union by size — components, cycle detection and Kruskal's minimum spanning tree at contest limits.

Module 8: Shortest paths: BFS, 0-1 BFS, Dijkstra

Choose the algorithm from the weights — BFS for equal edges, 0-1 BFS with a deque, Dijkstra with a priority_queue and lazy deletion — and write it right under a clock.

Module 9: Topological sort and DAGs

Kahn's algorithm with in-degrees, a min-heap for the smallest order, the cycle test by count, and dynamic programming in topological order: longest paths, path counts, semesters.

Module 10: Segment trees and Fenwick trees

Range queries with updates in O(log n): a segment tree for sums and minimums, a Fenwick tree in ten lines with i & -i, and counting inversions by value.

Module 11: DP optimisations

Make the transition cheap: prefix sums over the previous row, a monotonic deque for a sliding-window maximum, and bitmask DP over subsets of up to twenty items.

Module 12: String algorithms: KMP, Z and hashing

Linear-time matching with the KMP prefix function and the Z-function, borders and periods, and O(1) substring comparison with a double rolling hash.