Bird
Raised Fist0
Data Structures Theoryknowledge~6 mins

Common complexity classes (O(1), O(n), O(log n), O(n²)) in Data Structures Theory - Full Explanation

Choose your learning style10 modes available
Introduction
Imagine you want to know how long a task will take as the size of the input grows. Understanding common complexity classes helps predict how efficient an algorithm is and how it will perform with larger data.
Explanation
O(1) - Constant Time
An algorithm with O(1) complexity takes the same amount of time regardless of input size. It performs a fixed number of steps, like accessing a single item in a list by its position.
O(1) means the operation time does not change as input size grows.
O(n) - Linear Time
In O(n) complexity, the time grows directly in proportion to the input size. For example, checking each item in a list one by one takes longer as the list gets bigger.
O(n) means time increases linearly with input size.
O(log n) - Logarithmic Time
O(log n) complexity means the time grows slowly as input size increases, often by cutting the problem size in half each step. Searching in a sorted list by repeatedly dividing it is an example.
O(log n) means time grows slowly by reducing problem size each step.
O(n²) - Quadratic Time
With O(n²) complexity, time grows proportionally to the square of the input size. This happens when an algorithm compares every item with every other item, like in simple sorting methods.
O(n²) means time increases very quickly as input size grows.
Real World Analogy

Imagine sorting a deck of cards. If you pick one card without looking, it takes the same time no matter how many cards there are. If you look at each card once, time grows with the number of cards. If you split the deck in half repeatedly to find a card, it takes fewer steps. But if you compare every card to every other card, it takes much longer as the deck grows.

O(1) - Constant Time → Picking one card from a deck without looking at others
O(n) - Linear Time → Looking at each card one by one
O(log n) - Logarithmic Time → Splitting the deck in half repeatedly to find a card
O(n²) - Quadratic Time → Comparing every card with every other card
Diagram
Diagram
Input Size (n) →

O(1)   ──────────────■─────────────

O(log n) ─────■─────■────■────■────

O(n)   ■────■────■────■────■────■──

O(n²)  ■■■■■■■■■■■■■■■■■■■■■■■■■■■■
This diagram shows how time grows with input size for each complexity class, from constant to quadratic.
Key Facts
O(1) - Constant Time → Algorithm time does not change with input size.
O(n) - Linear Time → Algorithm time grows directly with input size.
O(log n) - Logarithmic Time → Algorithm time grows slowly by halving problem size each step.
O(n²) - Quadratic Time → Algorithm time grows with the square of input size.
Code Example
Data Structures Theory
def constant_time_example(lst):
    return lst[0]

def linear_time_example(lst):
    total = 0
    for item in lst:
        total += item
    return total

def logarithmic_time_example(lst, target):
    low, high = 0, len(lst) - 1
    while low <= high:
        mid = (low + high) // 2
        if lst[mid] == target:
            return mid
        elif lst[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return -1

def quadratic_time_example(lst):
    pairs = []
    for i in range(len(lst)):
        for j in range(len(lst)):
            pairs.append((lst[i], lst[j]))
    return pairs

# Example usage
print(constant_time_example([10, 20, 30]))
print(linear_time_example([1, 2, 3]))
print(logarithmic_time_example([1, 2, 3, 4, 5], 4))
print(len(quadratic_time_example([1, 2, 3])))
OutputSuccess
Common Confusions
Thinking O(1) means the algorithm is always fast.
Thinking O(1) means the algorithm is always fast. O(1) means time does not grow with input size, but the fixed time could still be large depending on the operation.
Believing O(log n) is faster than O(1).
Believing O(log n) is faster than O(1). O(1) is always faster for large inputs because it does not depend on input size, while O(log n) grows slowly but still increases.
Assuming O(n²) is acceptable for large inputs.
Assuming O(n²) is acceptable for large inputs. O(n²) grows very quickly and becomes impractical for large inputs, often needing more efficient algorithms.
Summary
Common complexity classes describe how algorithm time changes as input size grows.
O(1) is constant time, O(n) is linear, O(log n) is logarithmic, and O(n²) is quadratic.
Understanding these helps choose efficient algorithms for different problems.

Practice

(1/5)
1. Which complexity class describes an algorithm that takes the same amount of time regardless of input size?
easy
A. O(n)
B. O(n²)
C. O(log n)
D. O(1)

Solution

  1. Step 1: Understand what O(1) means

    O(1) means constant time, so the algorithm's time does not change with input size.
  2. Step 2: Compare with other options

    O(n), O(log n), and O(n²) all depend on input size and grow as input grows.
  3. Final Answer:

    O(1) -> Option D
  4. Quick Check:

    Constant time = O(1) [OK]
Hint: Constant time means no input size effect [OK]
Common Mistakes:
  • Confusing O(1) with O(n)
  • Thinking O(log n) is constant
  • Mixing up growth rates
2. Which of the following is the correct way to describe an algorithm with time complexity that doubles the work when the input size doubles?
easy
A. O(log n)
B. O(n²)
C. O(n)
D. O(1)

Solution

  1. Step 1: Understand linear growth

    Doubling work when the input size doubles means time grows directly with input size, which is linear.
  2. Step 2: Match with complexity classes

    O(n) means time grows linearly with input size, so it fits the description.
  3. Final Answer:

    O(n) -> Option C
  4. Quick Check:

    Linear growth = O(n) [OK]
Hint: Linear means time grows directly with input size [OK]
Common Mistakes:
  • Choosing O(log n) for linear growth
  • Confusing O(n²) with linear
  • Thinking O(1) grows with input
3. Consider the following code snippet:
for i in range(n):
    for j in range(n):
        print(i, j)

What is the time complexity of this code?
medium
A. O(n)
B. O(n²)
C. O(log n)
D. O(1)

Solution

  1. Step 1: Analyze the outer loop

    The outer loop runs n times, from 0 to n-1.
  2. Step 2: Analyze the inner loop

    For each iteration of the outer loop, the inner loop runs n times.
  3. Step 3: Calculate total iterations

    Total iterations = n (outer) * n (inner) = n².
  4. Final Answer:

    O(n²) -> Option B
  5. Quick Check:

    Nested loops each n times = O(n²) [OK]
Hint: Nested loops multiplying iterations = O(n²) [OK]
Common Mistakes:
  • Counting only one loop
  • Thinking nested loops are O(n)
  • Confusing with O(log n)
4. A programmer wrote this code to search for an element in a sorted list:
def search(lst, target):
    for i in range(len(lst)):
        if lst[i] == target:
            return True
    return False

What is the main issue with this code regarding time complexity?
medium
A. It uses O(n) but could be improved to O(log n)
B. It uses O(log n) but should be O(n)
C. It uses O(n²) unnecessarily
D. It uses O(1) which is too fast

Solution

  1. Step 1: Identify current complexity

    The code uses a simple loop over the list, so it is O(n).
  2. Step 2: Consider the list is sorted

    For sorted lists, binary search can be used to reduce time to O(log n).
  3. Final Answer:

    It uses O(n) but could be improved to O(log n) -> Option A
  4. Quick Check:

    Linear search on sorted list = O(n), binary search = O(log n) [OK]
Hint: Use binary search on sorted lists for O(log n) [OK]
Common Mistakes:
  • Thinking linear search is O(log n)
  • Confusing O(n²) with O(n)
  • Believing O(1) is possible here
5. You have a sorted list of 1,000,000 numbers and want to find if a number exists. Which algorithm choice is best for fastest average search time?
hard
A. Binary search on sorted list with O(log n) complexity
B. Linear search with O(n) complexity
C. Nested loops search with O(n²) complexity
D. Constant time search with O(1) complexity without extra data structures

Solution

  1. Step 1: Evaluate linear search

    Linear search checks each element, O(n) = 1,000,000 steps on average.
  2. Step 2: Evaluate binary search

    Binary search halves the search space each step, O(log n) ≈ 20 steps for 1,000,000 elements.
  3. Step 3: Evaluate nested loops and constant time

    Nested loops O(n²) is very slow; O(1) without extra structures is unrealistic here.
  4. Final Answer:

    Binary search on sorted list with O(log n) complexity -> Option A
  5. Quick Check:

    Binary search fastest for large sorted lists = O(log n) [OK]
Hint: Binary search is fastest for large sorted lists [OK]
Common Mistakes:
  • Choosing linear search for large data
  • Expecting O(1) without extra structures
  • Ignoring sorting requirement
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard