Basics for coding rounds · core
Compiling, headers and namespaces
g++ turns your .cpp file into a program in two steps: compile, then link. Headers bring in declarations, and namespaces keep names apart, so know what std:: and using namespace std really do.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
g++ -std=c++17 -Wall -O2 main.cpp -o maincompiles and links main.cpp into a program called main. -std picks the C++ version, -Wall turns on most warnings, -O2 turns on optimisation.- Two steps: the compiler turns each .cpp file into an object file, then the linker joins them. "undefined reference to f" is a linker error: f was declared, but its body was never compiled.
- #include pastes a header in before compiling. A header (.h or .hpp) holds declarations; the .cpp file holds the bodies. Include guards or #pragma once stop a header from being pasted twice.
- Since GCC 16, g++ uses C++20 by default; GCC 11 to 15 used C++17. C++23 is the current standard and is turned on with -std=c++23. Write the flag you need instead of trusting the default.
- The standard library lives in the namespace std, so the full names are std::cout, std::vector and std::sort. :: is the scope resolution operator.
using namespace std;makes every std name visible without std::. It is fine in a small contest file, but never put it in a header: every file that includes the header gets it too.- Name clashes are real: with
using namespace std;, a global variable named count clashes with std::count and gives an "ambiguous" compile error. Rename it, or write std:: where needed. - #include <bits/stdc++.h> is a GCC header that includes the whole library. It is not standard C++, so other compilers (MSVC, and clang on a Mac) may not have it, and it makes compiling slower.
The red pen
Common traps: mistake → fix
Mistake: using namespace std is needed for cout to work
Fix: it only saves typing; std::cout works without it.
Mistake: "undefined reference" is a mistake in the syntax of my code
Fix: it is a linker error: a declared function has no compiled body.
Mistake: bits/stdc++.h is part of standard C++
Fix: it is a GCC header that other compilers may not have.
Mistake: Every compiler uses the same C++ version by default
Fix: the default depends on the compiler and its version; pass -std.
Easy to hard
Questions interviewers ask, easy to hard
- What does
using namespace std;do, and why is it a bad idea in a header file? - What happens between writing main.cpp and running ./main?
- What is the difference between a compile error and a linker error? Give an example of each.
- Your code has a global variable called count and gets an "ambiguous" error. Why?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.