C++ and the STL
Use C++ with the Standard Template Library: the fastest and most common choice for placements and competitive programming.
Beginner · about 15 hours · difficulty 2/5
Learn first: How Campus and Off-Campus Hiring Works
C++ is the default language for competitive programming and most Indian placement OAs. It is fast, so slightly heavier solutions still pass, and the STL gives you every data structure you need.
The subset you need
- Types and limits:
intholds about 2.1e9,long longabout 9.2e18. Uselong longfor sums and products. - Containers:
vector,string,pair,stack,queue,deque,priority_queue,set,multiset,map,unordered_map,unordered_set. - Algorithms:
sortwith lambdas,lower_bound,upper_bound,reverse,accumulate,next_permutation,__gcdorstd::gcd. - Pass large containers by reference:
const vector<int>& a. - Fast I/O:
ios::sync_with_stdio(false); cin.tie(nullptr);
Pitfalls
a * bcomputed inintoverflows before it is stored into along long. Cast one side first.v.size() - 1on an empty vector wraps to a huge unsigned number.mp[key]inserts a default entry; usecountorfindto test membership.unordered_mapcan be hacked to O(n) per operation on Codeforces; usemapor a custom hash there.
You are ready to move on when you can write a frequency map, sort pairs by second value with a lambda and build a min-heap without looking anything up.
Key concepts
- Types, overflow and long long
- vector, string, pair
- set, map, multiset: Ordered, O(log n)
- unordered_map and unordered_set
- stack, queue, deque, priority_queue
- sort with lambdas and comparators
- lower_bound and upper_bound
- Fast I/O and contest template
Other options for this step
- Java and the Collections Framework: Use Java with ArrayList, HashMap, ArrayDeque and PriorityQueue: a strong choice if your college teaches Java or you target Java backend roles.
- Python for DSA: Use Python with collections, heapq and bisect: the quickest language to write, fine for interviews but sometimes too slow for strict OAs.
Mini project
Write a contest template, then solve 8 warm-ups using only STL: word frequency, sort students by marks then name, k largest with a heap, remove duplicates with set, anagram check, first element >= x with lower_bound, next permutation, and running median with two heaps.
Check yourself
Why can passing a vector<int> by value into a recursive function cause TLE, and what is the one-word fix?
Free resources
- cppreference: containers library (cppreference)
- LearnCpp.com (LearnCpp)
- Competitive Programmer's Handbook (CSES)