Notifications

No notifications

/Phase 4

Practice โ€” Your C++ Roadmap

You Know C++. Now What? ๐Ÿ

You've covered syntax โ†’ OOP โ†’ memory โ†’ templates โ†’ STL โ†’ modern features. The remaining skill is pattern fluency โ€” recognising which tool fits a problem in seconds. That comes from solving 200+ problems, not from reading.

A Sane Daily Routine

BlockWhatTime
Warm-upOne Easy on LeetCode/HackerRank15 min
FocusOne Medium in a topic you're learning45 min
ReadingEditorial of yesterday's Medium15 min

One hour, five days a week. That's enough.

Where to Practice (Ranked)

1. LeetCode โ€” Best UI, best community editorials, interview-aligned. 2. HackerRank โ€” C++ track โ€” Beginner-friendly, language-specific. 3. Codeforces โ€” Real competitive programming, weekly contests. 4. AtCoder Beginner Contests (ABCs) โ€” Crystal-clear problem statements, gentle difficulty curve. 5. CSES Problem Set โ€” Hand-picked classic algorithms, no fluff.

A 4-Week Plan

WeekFocusDaily set
1Arrays + stringsTwo Sum, Best Time to Buy Stock, Reverse String, Valid Anagram
2Hashmap + two-pointerGroup Anagrams, Top K Frequent, Move Zeroes, 3Sum
3Sorting + binary searchSort Colors, Search Insert, Find First/Last, Median of Two Arrays
4Graph/Tree basicsBFS, DFS, Number of Islands, LCA

On this page

Detailed Theory

Reaching the practice phase means you stop *learning the language* and start *thinking algorithmically in it*. C++ is a fantastic vehicle here โ€” fast enough that brute force often passes, expressive enough that elegant code is short.

How to Use C++ in Competitive / Interview Settings

A Solid Boilerplate

#include <bits/stdc++.h>
using namespace std;

int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr);

int n; cin >> n; vector<int> a(n); for (auto& x : a) cin >> x;

// ...

return 0; }

  • includes the entire STL (g++ extension) โ€” fine for contests, avoid in production.
  • sync_with_stdio(false) + cin.tie(nullptr) makes cin/cout ~10ร— faster.
  • Read everything into vectors; iterate with range-for.

Compilation Flags You Should Know

# Day-to-day
g++ -std=c++20 -O2 -Wall -Wextra solution.cpp -o sol

# Heavy debugging g++ -std=c++20 -O0 -g -Wall -Wextra -fsanitize=address,undefined solution.cpp -o sol

-fsanitize=address,undefined is the killer flag โ€” catches buffer overruns, use-after-free, signed overflow at runtime.

How to Solve a Problem (5-Step Loop)

1. Read it twice. Note inputs, outputs, constraints. Check if N โ‰ค 100 (any algorithm), 10โต (need O(n log n)), 10โธ (O(n) only). 2. Solve by hand on a tiny example. What pattern emerges? 3. Pick a data structure. Need fast lookup โ†’ unordered_map. Need sorted โ†’ set / priority_queue. Stack-like โ†’ stack or vector::back(). 4. Write & test on edge cases. Empty input, all same, all different, max constraint. 5. Read the editorial AFTER, even if you solved it. That's where you find the elegant solution and learn new patterns.

The Topic Tree to Master

Phase A โ€” Fundamentals (50 problems)
โ”œโ”€โ”€ Array / String manipulation
โ”œโ”€โ”€ Hashmap counting
โ”œโ”€โ”€ Two pointers
โ”œโ”€โ”€ Sliding window
โ””โ”€โ”€ Prefix sums

Phase B โ€” Sorting & Searching (40 problems) โ”œโ”€โ”€ Sort + scan โ”œโ”€โ”€ Binary search on sorted array โ”œโ”€โ”€ Binary search on the answer โ””โ”€โ”€ Heap / priority_queue (top-k)

Phase C โ€” Recursion & DP (60 problems) โ”œโ”€โ”€ Recursion โ†’ memoization โ†’ DP table โ”œโ”€โ”€ Knapsack family โ”œโ”€โ”€ LIS / LCS โ””โ”€โ”€ Interval DP

Phase D โ€” Graphs (30 problems) โ”œโ”€โ”€ BFS / DFS โ”œโ”€โ”€ Topological sort โ”œโ”€โ”€ Union-Find (DSU) โ””โ”€โ”€ Dijkstra (priority_queue)

Phase E โ€” Advanced (20 problems) โ”œโ”€โ”€ Segment tree / Fenwick (BIT) โ”œโ”€โ”€ Trie โ”œโ”€โ”€ Math (gcd / sieve / mod inverse) โ””โ”€โ”€ String (KMP / Z / hashing)

Patterns You'll See in 80% of Problems

Two Pointers

int l = 0, r = n - 1;
while (l < r) {
    if (arr[l] + arr[r] == target) return {l, r};
    if (arr[l] + arr[r] < target) ++l;
    else                          --r;
}

Sliding Window

int l = 0, best = 0, sum = 0;
for (int r = 0; r < n; ++r) {
    sum += arr[r];
    while (sum > limit) sum -= arr[l++];
    best = max(best, r - l + 1);
}

Hashmap Counting

unordered_map<int, int> seen;
for (int i = 0; i < n; ++i) {
    int need = target - arr[i];
    if (seen.count(need)) return {seen[need], i};
    seen[arr[i]] = i;
}

BFS

queue<pair<int,int>> q;
vector<vector<bool>> visited(R, vector<bool>(C, false));
q.push({sr, sc}); visited[sr][sc] = true;

const int dr[] = {-1, 1, 0, 0}; const int dc[] = { 0, 0,-1, 1}; while (!q.empty()) { auto [r, c] = q.front(); q.pop(); for (int d = 0; d < 4; ++d) { int nr = r + dr[d], nc = c + dc[d]; if (nr < 0

nr >= Rnc < 0
nc >= C) continue; if (visited[nr][nc]) continue; visited[nr][nc] = true; q.push({nr, nc}); } }

DFS via Recursion

function<void(int)> dfs = [&](int u) {
    visited[u] = true;
    for (int v : adj[u]) if (!visited[v]) dfs(v);
};

Dijkstra (priority_queue)

priority_queue<pair<long,int>, vector<pair<long,int>>, greater<>> pq;
vector<long> dist(n, LLONG_MAX);
dist[src] = 0;
pq.push({0, src});
while (!pq.empty()) {
    auto [d, u] = pq.top(); pq.pop();
    if (d > dist[u]) continue;
    for (auto [v, w] : adj[u]) {
        if (dist[u] + w < dist[v]) {
            dist[v] = dist[u] + w;
            pq.push({dist[v], v});
        }
    }
}

DP โ€” Memoization Skeleton

vector<int> memo(n + 1, -1);
function<int(int)> dp = [&](int i) -> int {
    if (i <= 1) return i;
    if (memo[i] != -1) return memo[i];
    return memo[i] = dp(i - 1) + dp(i - 2);
};

Suggested Books / References

TitleWhy
A Tour of C++ by Bjarne StroustrupAuthor's own modern overview, ~200 pages
Effective Modern C++ by Scott Meyers42 items, every modern feature explained with pitfalls
C++ Primer by Lippman et al.Comprehensive language reference
Competitive Programmer's Handbook by Antti LaaksonenFree PDF, every CP topic with C++ examples
CppReference.comThe reference. Bookmark it.

Build a Portfolio As You Learn

  • Start a public GitHub repo: leetcode-solutions. One file per problem.
  • README each folder by topic.
  • After every batch of 10 problems, write a short markdown "what I learned".
  • After 100 problems, you'll have an interview-quality portfolio without trying.

When You're Ready to Specialise

DirectionNext steps
Game / graphicsLearn OpenGL or Vulkan, ECS patterns, math (linalg, quaternions)
EmbeddedARM Cortex toolchains, no-heap C++ subset, RTOS
HFT / quantLock-free data structures, low-latency I/O, profiling
CompilersLLVM, Clang AST tools
SystemsLinux kernel API, ZeroMQ, Folly, Boost

Final Loop: Solve ยท Read Editorial ยท Re-solve

1. Solve a problem on your own. 2. Read the editorial โ€” even if you solved it. 3. Solve the same problem again, the editorial's way.

That third step is where the patterns lodge. Skip it and you re-invent the wheel forever.

You finished the C++ track. Build something โ€” a small game, a CLI tool, an HTTP server. Real projects compress months of practice into weeks. Good luck. ๐Ÿš€