Time Complexity

Learn Big O notation and analyze how algorithm runtime scales with input size.

0 Problems1 hour

Overview

Time complexity measures how the runtime of an algorithm grows as the input size n grows. We use Big O notation to express this growth rate, ignoring constants and lower-order terms. Understanding time complexity lets you predict whether your solution will be fast enough for large inputs.

Complexity Summary

Time Complexity

This topic teaches how to measure time complexity

Space Complexity

N/A

Key Patterns & Techniques

Learn the core patterns in this topic. Each block explains when to use the pattern, the intuition behind it, and a compact code example.

1

O(1)

Concept

constant time regardless of input.

Pattern Example
Problem

map.get(key), arr[i]

Practice questions for this pattern
2

O(log n)

Concept

input halved each step.

Pattern Example
Problem

while (n > 1) { n = Math.floor(n/2); }

Practice questions for this pattern
3

O(n)

Concept

single pass through input.

Pattern Example
Problem

for (const x of arr) { ... }

Practice questions for this pattern
4

O(n log n)

Concept

sorting or divide-and-conquer.

Pattern Example
Problem

arr.sort(), merge sort

Practice questions for this pattern
5

O(n²)

Concept

nested loops over same input.

Pattern Example
Problem

for i: for j: compare arr[i] and arr[j]

Practice questions for this pattern
6

O(2ⁿ)

Concept

exponential, all subsets.

Pattern Example
Problem

function fib(n) { return fib(n-1)+fib(n-2); }

Practice questions for this pattern
7

Drop constants

Concept

O(3n) = O(n)

Why It Works

Keep only the dominant term

Practice questions for this pattern
8

Worst case

Concept

Big O always describes worst-case unless stated otherwise

Practice questions for this pattern
DSA Practice Online - 150+ Coding Interview Questions | LeetCode Alternative | InstaMock - AI Mock Interview