Skip to main content

Functions

Functions help you organize code, avoid repetition, and make your solutions cleaner.

Defining functions

Pass by reference vs value

Always pass vectors and strings by reference (&) to avoid copying. Use const& if you don’t need to modify them.

Structs

Structs let you group related data together.

Sorting structs

Pairs and tuples

Time complexity

Understanding time complexity tells you whether your solution will pass within the time limit.

Big-O notation

How to estimate

The rule of thumb: 10⁸ simple operations per second.
Before coding, always check the constraints and calculate if your approach is fast enough. A TLE (Time Limit Exceeded) means your algorithm is too slow.

Analyzing loops

Practice

Sheet 4: Functions, Structs & Time Complexity

Practice problems for this tutorial.