OOPs HandbookTime and Space Complexity for Coding Interviews: Big-O with Examples
Coding InterviewsBeginner
8 min read

Time and Space Complexity for Coding Interviews: Big-O with Examples

How to work out the Big-O of your own code, the common complexities, and how to use input limits to pick an algorithm.

TM
TestMeMan SDE Curriculum
Updated Sept 2026
Verified for FAANG Interviews
FROM TESTMEMAN•TestMeMan Campus

Coding problems with instant test-case feedback

Write Python, C++, C or JavaScript in your browser and see which hidden tests pass.

Sponsored Publisher Placement•
ID: ad-top-leaderboard
Executive Summary

Big-O describes how the running time or memory of an algorithm grows as the input grows. In a coding round it decides whether your solution passes the hidden tests or times out, and interviewers almost always ask "what is the complexity of this?" after you code.

1. The rules for calculating Big-O#

  • Count how many times the most repeated statement runs, as a function of n.
  • Drop constants: 3n + 5 is O(n).
  • Keep only the fastest-growing term: n² + n is O(n²).
  • Sequential blocks add (O(n) + O(n) = O(n)); nested loops multiply (O(n) inside O(n) = O(n²)).
  • Different inputs get different letters: looping over an array of size n and a string of size m is O(n + m), not O(n).

2. Common complexities with examples#

ComplexityNameTypical example
O(1)ConstantArray access, hash map lookup (average)
O(log n)LogarithmicBinary search; each step halves the range
O(n)LinearOne pass over an array
O(n log n)LinearithmicMerge sort, sorting with the standard library
O(n²)QuadraticComparing every pair with two nested loops
O(2ⁿ)ExponentialGenerating all subsets
O(n!)FactorialGenerating all permutations
Sponsored ContentTestMeMan Campus

Practise placement aptitude on the real test pattern

TCS NQT, Infosys, Wipro, Accenture and Cognizant style mocks with section-wise analysis.

3. Same problem, three complexities#

Problem: does any pair in the array add up to target? The brute force checks all pairs in O(n²). Sorting plus two pointers is O(n log n). A hash set gives O(n) time at the cost of O(n) extra space.

1#include <unordered_set>
2#include <vector>
3
4bool hasPair(const std::vector<int>& nums, int target) { // O(n) time, O(n) space
5 std::unordered_set<int> seen;
6 for (int x : nums) {
7 if (seen.count(target - x)) return true;
8 seen.insert(x);
9 }
10 return false;
11}
Analysis: Trading memory for time is the most common optimisation in interviews.

4. Use the constraints to choose the approach#

Online judges run roughly 10⁸ simple operations per second. Read the input limit before coding and pick a complexity that fits.

Largest nComplexity that usually passes
n ≤ 10–12O(n!) or O(2ⁿ · n)
n ≤ 20–25O(2ⁿ)
n ≤ 500O(n³)
n ≤ 5,000O(n²)
n ≤ 10⁶O(n log n) or O(n)
n ≤ 10¹⁸O(log n) or O(1) (maths, binary search)
Space counts too

Recursion uses stack space: a recursive DFS on a path of length n uses O(n) space even with no extra arrays. Very deep recursion can overflow the stack in C++ and Java.

Test Your Concept Mastery

Verify your retention on: Time and Space Complexity for Coding Interviews: Big-O with Examples

Diagnostic Check
Question 1 of 2
What is the time complexity of a loop where i starts at 1 and doubles each step until it reaches n?
Question 2 of 2
With n up to 10⁵, which approach is safe for a 1-second limit?
Ready for a full proctored assessment?

Take a free aptitude mock on a real company pattern, with a worked solution for every question.

Take an aptitude mock