Skip to main content
LeetCode Prep
CHAPTER 02 Beginner

Time Complexity and Big-O Analysis

Updated: May 18, 2026
5 min read

# CHAPTER 2

Time Complexity and Big-O Analysis

1. Chapter Introduction

In a coding interview, getting the correct answer is only half the battle. The interviewer will immediately ask: "What is the Time and Space complexity of your solution?" If you cannot answer using Big-O notation, you will fail the round. Big-O is the mathematical language used by software engineers to describe how an algorithm scales as the input size grows. This chapter demystifies Big-O, breaking down constant, linear, and quadratic time complexities.

2. What is Big-O Notation?

Big-O notation describes the worst-case scenario for an algorithm's efficiency. We don't measure time in seconds (because a supercomputer runs code faster than a laptop). Instead, we measure the *number of operations* relative to the size of the input, denoted as N.
  • Time Complexity: How much longer does the algorithm take as N grows?
  • Space Complexity: How much extra RAM/Memory does the algorithm use as N grows?

3. Common Time Complexities (From Best to Worst)

#### 1. O(1) - Constant Time The algorithm takes the exact same amount of time regardless of how big N is. *Analogy:* Determining if a number is even or odd. It takes 1 step whether the number is 10 or 10 billion.

python
12
def get_first_element(nums):
    return nums[0] # Always 1 step, O(1) Time

#### 2. O(log N) - Logarithmic Time The algorithm cuts the problem size in half with every step. Extremely fast. *Analogy:* Looking up a word in a dictionary. You open to the middle, check if the word is in the left or right half, and discard the other half. *Example:* Binary Search. (If N is 1 million, it takes only ~20 steps to find the answer).

#### 3. O(N) - Linear Time The time scales 1-to-1 with the input. If the input doubles, the time doubles. *Analogy:* Reading every single page in a book from start to finish.

python
1234
def print_all_elements(nums):
    for num in nums: # Loops N times
        print(num)
# Time: O(N)

#### 4. O(N log N) - Linearithmic Time Slightly slower than O(N). This is the absolute best time complexity you can achieve when sorting an array. *Example:* Merge Sort, Quick Sort, or calling the built-in .sort() function in Python/Java.

#### 5. O(N^2) - Quadratic Time (Often a Brute Force) For every element in the input, you loop through the input *again*. If N is 1,000, it takes 1,000,000 operations. *Analogy:* Comparing every person in a room to every other person in the room to find matching birthdays.

python
12345
def print_pairs(nums):
    for i in range(len(nums)):
        for j in range(len(nums)): # Nested loop
            print(nums[i], nums[j])
# Time: O(N^2)

#### 6. O(2^N) and O(N!) - Exponential / Factorial Time Horrendously slow. Will crash your computer if N > 30. *Example:* Unoptimized recursive Fibonacci, generating all permutations of a string.

4. Space Complexity

Space complexity works exactly the same way, but instead of counting operations, we count *extra memory allocation*.
  • Modifying an array in-place requires zero extra memory = O(1) Space.
  • Creating a new array to hold the result of size N = O(N) Space.
  • A recursive function that makes N nested calls uses the Call Stack = O(N) Space.

5. Big-O Calculation Rules

  1. 1. Drop the Constants: O(2N) is simplified to O(N). O(N/2) is O(N). We only care about the curve, not the exact math.
  1. 2. Drop the Non-Dominant Terms: If an algorithm has an O(N^2) loop followed by an O(N) loop, the total time is O(N^2 + N). We drop the N. The final complexity is O(N^2) because as N approaches infinity, the N term becomes irrelevant.

6. Real-World Scenario: The Brute Force Optimization

*Problem:* Given an array of numbers, return True if any value appears at least twice. *Brute Force Approach:*
python
1234567
# Compare every element to every other element
def containsDuplicate_Brute(nums):
    for i in range(len(nums)):
        for j in range(i + 1, len(nums)):
            if nums[i] == nums[j]: return True
    return False
# Time: O(N^2), Space: O(1)

*Optimized Approach (Using a Hash Set):*

python
1234567
def containsDuplicate_Optimized(nums):
    seen = set()
    for num in nums:
        if num in seen: return True
        seen.add(num)
    return False
# Time: O(N), Space: O(N)

*The Interview Tradeoff:* We drastically improved the Time speed (from N^2 to N) by sacrificing Space (using extra RAM to build a Set). This "Time/Space Tradeoff" is the core of computer science.

7. Complexity Visualized

text
123456789
Operations
   ^
   |                     / O(2^N) - Exponential
   |                   /
   |                 / O(N^2) - Quadratic
   |               /
   |             /   O(N) - Linear
   |----------------------- O(1) - Constant
   +--------------------------------> Input Size (N)

8. Mini Project: Complexity Analyzer

Analyze the following code:
javascript
123456789
function analyzeMe(arr) {
    arr.sort(); // O(N log N)
    
    let result = []; // Requires O(N) space
    for (let i = 0; i < arr.length; i++) { // O(N)
        result.push(arr[i] * 2);
    }
    return result;
}

*Total Time:* O(N log N + N). We drop the non-dominant N. Time = O(N log N). *Total Space:* Creating an array of size N. Space = O(N).

9. Common Mistakes

  • Confusing O(N) with O(1): Assuming that finding an element in a Python List/Array is O(1). Searching an array requires looking at every element, so if x in array: is O(N). (Searching a Hash Set is O(1)).
  • Forgetting String Concatenation: In many languages (Java, Python), strings are immutable. Doing string += "a" inside a loop of size N creates a new string every time, resulting in an accidental O(N^2) time complexity.

10. Best Practices

  • Always State Constraints: If an interviewer says N <= 10^4, an O(N^2) algorithm will take ~10^8 operations (approx 1 second) and might pass. If N <= 10^5, O(N^2) will take 10 billion operations and will Time Out. You must use O(N) or O(N log N).

11. Exercises

  1. 1. What is the time complexity of two separate, un-nested loops that each run N times?
  1. 2. Why is Binary Search O(log N) instead of O(N)?

12. MCQs

Question 1

What does Big-O notation measure?

Question 2

Which time complexity represents the absolute fastest possible algorithm?

Question 3

If your code contains a standard for loop that iterates through every element in an array of size N, what is the Time Complexity?

Question 4

If your code contains a for loop nested inside another for loop (both iterating to N), what is the Time Complexity?

Question 5

According to Big-O rules, how do you simplify O(2N + 5)?

Question 6

What does "Space Complexity" measure?

Question 7

If you use the built-in .sort() function on an array in Python or Java, what Time Complexity does it generally add to your algorithm?

Question 8

How do you simplify the expression O(N^2 + N)?

Question 9

What is a classic example of O(log N) time complexity?

Question 10

What is the fundamental "Tradeoff" often made in technical interviews to optimize an algorithm from O(N^2) to O(N)?

14. Interview Questions

  • Q: "You have an algorithm that takes O(N) Time and O(N) Space. Can you optimize it to O(1) Space? What time complexity tradeoff might that require?"

15. FAQs

  • Q: Do I need to know Big-Omega or Big-Theta?
A: For 99% of interviews, no. Big-O (Worst-case) is the universal language expected by FAANG interviewers.

16. Summary

Mastering Big-O notation is non-negotiable. You must be able to glance at a piece of code and instantly know if it is O(1), O(log N), O(N), or O(N^2). Remember the rules: drop constants, drop non-dominant terms, and account for extra memory allocation when calculating Space Complexity. Most interviews revolve around finding an O(N^2) brute force solution and utilizing a Hash Map or sorting technique to optimize it down to O(N) or O(N log N).

17. Next Chapter Recommendation

Now that we have the mathematical vocabulary, we can tackle the most fundamental data structure in computer science. In Chapter 3: Arrays and Strings Problems, we will explore traversal, prefix sums, and frequency counting.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·