🧮

Complexity Practice

Practice analyzing time and space complexity of code snippets and algorithms.

0 Problems1 hour

Overview

Reading code and quickly determining its complexity is a key interview skill. This section walks through worked examples — nested loops, recursion, and combined patterns — so you can confidently analyze any code you write or are asked about.

Complexity Summary

Time Complexity

Practice topic — no single answer

Space Complexity

Practice topic — no single answer

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

Single loop

Concept

O(n).

Pattern Example
Problem

for (let i=0; i<n; i++) → O(n)

Practice questions for this pattern
2

Nested loops same variable

Concept

O(n²).

Pattern Example
Problem

for i: for j: → O(n²)

Practice questions for this pattern
3

Two separate loops

Concept

O(n)+O(n) = O(n).

Pattern Example
Problem

loop1 then loop2 → still O(n)

Practice questions for this pattern
4

Loop halving input

Concept

O(log n).

Pattern Example
Problem

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

Practice questions for this pattern
5

Recursion two branches

Concept

O(2ⁿ).

Pattern Example
Problem

fib(n-1)+fib(n-2) → O(2ⁿ) without memo

Practice questions for this pattern
6

Recursion halving

Concept

O(log n) or O(n log n).

Pattern Example
Problem

mergeSort splits then merges → O(n log n)

Practice questions for this pattern
7

Combined

Concept

keep dominant term.

Pattern Example
Problem

O(n²)+O(n) = O(n²)

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