Topzee logo
Open menu
DSA Interview Prep

Time Complexity Cheat Sheet: O(1) to O(n^2) With Real Examples

A quick-reference cheat sheet for common time complexities, each with a real code example.

Topzee3 min read
Abstract time complexity cheat sheet cover with layered code rows and chart lines.

This time complexity cheat sheet is for the moment when you understand the idea of Big O, but still need quick examples you can recognize in code. The goal is not to memorize symbols. The goal is to spot patterns fast during practice and interviews.

Here are the common tiers from O(1) to O(n^2), with short examples and recognition tips.

Time Complexity Cheat Sheet Table

Complexity Name Example operation
O(1) Constant Read an array item by index
O(log n) Logarithmic Binary search
O(n) Linear Scan every item once
O(n log n) Linearithmic Efficient sorting
O(n^2) Quadratic Compare every pair

O(1): Constant

function getLast<T>(items: T[]): T | undefined {
  return items[items.length - 1];
}

The function does one direct lookup. The input can grow, but the work does not grow with it.

Interview tip: look for direct access, fixed-size work, and no loop based on input size.

O(log n): Logarithmic

function binarySearch(nums: number[], target: number): number {
  let left = 0;
  let right = nums.length - 1;

  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    if (nums[mid] === target) return mid;
    if (nums[mid] < target) left = mid + 1;
    else right = mid - 1;
  }

  return -1;
}

The search space is cut in half each step.

Interview tip: if each iteration removes a fraction of the remaining input, think O(log n).

O(n): Linear

function sum(nums: number[]): number {
  let total = 0;

  for (const num of nums) {
    total += num;
  }

  return total;
}

Every item is visited once.

Interview tip: one pass over the input is usually O(n), even if there are several constant-time operations inside the loop.

O(n log n): Linearithmic

Efficient comparison-based sorting is commonly O(n log n).

function sortedScores(scores: number[]): number[] {
  return [...scores].sort((a, b) => a - b);
}

In JavaScript and TypeScript, you usually rely on the built-in sort, but in interviews you should still know that sorting often costs O(n log n).

Interview tip: if your solution starts with sorting, include that cost in the final complexity.

O(n^2): Quadratic

function countPairs(nums: number[]): number {
  let pairs = 0;

  for (let i = 0; i < nums.length; i += 1) {
    for (let j = i + 1; j < nums.length; j += 1) {
      if (nums[i] + nums[j] > 10) {
        pairs += 1;
      }
    }
  }

  return pairs;
}

Nested loops over the same input often mean quadratic time.

Interview tip: if every item is compared with many other items, pause and ask whether a hash map, sorting, or two pointers can reduce the work.

Do Constants Matter?

For Big O, constants are dropped. O(2n) becomes O(n). O(100) becomes O(1). This does not mean constants never matter in real performance, but interviews usually focus on the growth category.

function printTwice(items: string[]) {
  for (const item of items) console.log(item);
  for (const item of items) console.log(item);
}

This is O(n), not O(2n), because both loops scale linearly.

Quick Practice Routine

Pick one problem after every practice session and write:

  • Time complexity
  • Space complexity
  • The line or structure that causes it
  • One possible optimization idea

That small routine makes complexity analysis less stressful over time.

If this cheat sheet helps, use it as a review card before mock interviews. The real win is being able to explain why a solution has a certain complexity.