Big O Calculator

Free Big O notation calculator. Paste your code and calculate Big O time and space complexity instantly. Supports Python, JavaScript, Java, C++, and 9 more languages.

Try an example

0/2000

Big O Complexity Reference

NotationName
O(1)Constant
O(log n)Logarithmic
O(n)Linear
O(n log n)Linearithmic
O(n²)Quadratic
O(n³)Cubic
O(2^n)Exponential
O(n!)Factorial

How to Calculate Big O with This Tool

Select your programming language, paste your code, and click Analyze Complexity. This Big O calculator will determine both the time complexity and space complexity of your code, with a step-by-step explanation of how it arrived at the result.

Use this Big O notation calculator to analyze common patterns like nested loops (O(n²)), binary search (O(log n)), divide-and-conquer recursion (O(n log n)), and hash map operations (O(1) amortized). You can calculate Big O for real code, not just textbook examples.

Preparing for coding interviews? Pair this tool with our Big O cheat sheet for a complete complexity reference, or practice with Blind 75 problems and calculate Big O for your solutions here.

Common Big O Patterns with Code

The 8 complexity patterns you will see in every coding interview, with annotated code.

O(1)Hash Map Lookup

Direct key access. No matter how large the map, lookup is constant time on average.

Time: O(1)|Space: O(n)
Python
def two_sum(nums, target):
    seen = {}
    for i, num in enumerate(nums):
        if target - num in seen:     # O(1) lookup
            return [seen[target - num], i]
        seen[num] = i                # O(1) insert
O(log n)Binary Search

Halves the search space every iteration. Doubling the input only adds one extra step.

Time: O(log n)|Space: O(1)
Python
def binary_search(arr, target):
    lo, hi = 0, len(arr) - 1
    while lo <= hi:
        mid = (lo + hi) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            lo = mid + 1           # discard left half
        else:
            hi = mid - 1           # discard right half
    return -1
O(n)Single Pass

Visits every element exactly once. The most common linear-time pattern.

Time: O(n)|Space: O(1)
Python
def find_max(arr):
    max_val = arr[0]
    for num in arr:              # one pass through n elements
        if num > max_val:
            max_val = num
    return max_val
O(n log n)Sorting (Merge Sort)

Divide-and-conquer: split in half (log n levels), merge all elements at each level (n work).

Time: O(n log n)|Space: O(n)
Python
def merge_sort(arr):
    if len(arr) <= 1:
        return arr
    mid = len(arr) // 2
    left = merge_sort(arr[:mid])     # T(n/2)
    right = merge_sort(arr[mid:])    # T(n/2)
    return merge(left, right)        # O(n) merge
O(n²)Nested Loops

Every pair is checked. Common in brute-force solutions — the first thing to optimize.

Time: O(n²)|Space: O(1)
Python
def has_duplicate(arr):
    for i in range(len(arr)):
        for j in range(i + 1, len(arr)):  # n * (n-1) / 2
            if arr[i] == arr[j]:
                return True
    return False
O(2^n)Recursive Subsets

Each element is either included or excluded — two choices per element, 2^n total subsets.

Time: O(2^n)|Space: O(n)
Python
def subsets(nums):
    result = []
    def backtrack(start, current):
        result.append(current[:])
        for i in range(start, len(nums)):
            current.append(nums[i])     # include
            backtrack(i + 1, current)
            current.pop()               # exclude
    backtrack(0, [])
    return result
O(V + E)BFS / DFS on Graph

Visit every vertex once (V) and traverse every edge once (E). Standard graph traversal.

Time: O(V + E)|Space: O(V)
Python
def bfs(graph, start):
    visited = set()
    queue = [start]
    visited.add(start)
    while queue:
        node = queue.pop(0)              # process vertex
        for neighbor in graph[node]:     # check edges
            if neighbor not in visited:
                visited.add(neighbor)
                queue.append(neighbor)
O(n)Sliding Window

Two pointers move forward only — each element is added and removed at most once.

Time: O(n)|Space: O(1)
Python
def max_subarray_sum(arr, k):
    window = sum(arr[:k])
    best = window
    for i in range(k, len(arr)):
        window += arr[i] - arr[i - k]   # slide right
        best = max(best, window)
    return best

FAQ

What is a Big O calculator?add

A Big O calculator is a tool that analyzes your code and determines its time and space complexity using Big O notation. This Big O notation calculator examines loops, recursion, and data structure operations to classify how your algorithm scales with input size, from O(1) constant time to O(n!) factorial time.

How do I calculate Big O of my code?add

To calculate Big O, paste your code into the editor above, select the programming language, and click Analyze Complexity. The Big O calculator will break down your code step by step: counting loop iterations, identifying recursion depth, and evaluating data structure operations, then return the overall time and space complexity.

What programming languages are supported?add

This Big O notation calculator supports Python, JavaScript, TypeScript, Java, C++, C, C#, Go, Rust, Ruby, Swift, Kotlin, and pseudocode. You can calculate Big O for code in any of these languages and get an accurate complexity analysis.

How accurate is the Big O analysis?add

The Big O calculator is highly accurate for standard patterns: nested loops, binary search, divide-and-conquer recursion, hash map lookups, and common sorting algorithms. For complex algorithms with amortized analysis or non-obvious recurrences, treat the result as a strong starting point and verify manually.

What is the difference between time and space complexity?add

Time complexity measures how the number of operations grows as input size increases. Space complexity measures how much additional memory your algorithm uses. For example, merge sort has O(n log n) time complexity but O(n) space complexity because it creates temporary arrays during merging. Use this Big O calculator to calculate both for any code snippet.

Why does Big O matter for coding interviews?add

Every coding interview at top tech companies (Google, Meta, Amazon, etc.) expects you to calculate Big O for your solution. Interviewers want to see that you can write efficient code and understand the tradeoffs between different approaches. Saying 'my solution is O(n log n) time and O(1) space' is expected.

What are the most common Big O complexities?add

From fastest to slowest: O(1) constant (hash map lookup), O(log n) logarithmic (binary search), O(n) linear (single loop), O(n log n) linearithmic (merge sort, heap sort), O(n²) quadratic (nested loops), O(2^n) exponential (recursive subsets), O(n!) factorial (permutations). Use the Big O notation calculator above to identify which one your code falls into.

Know your complexity? Now practice.

Simulate real coding interviews with an AI interviewer.

Try freearrow_forward