Flashcard library · Computer Science

Computer Science: Big-O Notation

Master the fundamentals of Big-O Notation with this essential flashcard deck. Designed for computer science students, these cards cover definitions, common complexities, and practical rules for analyzing algorithm efficiency. Understand why Big-O is crucial for writing performant code and excel in your studies.

Want to actually learn these?

Create a free NoteFren account to study with spaced repetition, or turn your own notes into cards like these.

What is Big O Notation?

Big O Notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity. In computer science, it specifically characterizes an algorithm's performance or complexity in terms of its input size.

What does Big O Notation primarily describe about an algorithm?

It primarily describes the upper bound or worst-case scenario of an algorithm's running time or space complexity as the input size grows, ignoring constant factors and smaller terms.

Why is understanding Big O Notation important in computer science?

It allows developers to evaluate and compare the efficiency of different algorithms, predict how an algorithm will scale with larger inputs, and choose the most performant solution for a given problem.

In Big O Notation, what does 'n' typically represent?

'n' typically represents the size of the input to the algorithm. For example, it could be the number of items in a list, the number of nodes in a tree, or the length of a string.

Describe an algorithm with O(1) complexity and provide an example.

O(1) or constant time complexity means an algorithm takes the same amount of time to execute regardless of the input size. An example is accessing an element at a specific index in an array.

Describe an algorithm with O(log n) complexity and provide an example.

O(log n) or logarithmic time complexity means the execution time grows very slowly as the input size increases, often by repeatedly halving the problem size. Binary search is a classic example of an O(log n) algorithm.

Describe an algorithm with O(n) complexity and provide an example.

O(n) or linear time complexity means the execution time grows proportionally to the input size. Iterating through all elements in a list once to find a specific value is an O(n) operation.

Describe an algorithm with O(n log n) complexity and provide an example.

O(n log n) or linearithmic time complexity often arises in algorithms that divide a problem into subproblems, solve them, and combine the results. Merge Sort and Quick Sort (average case) are common examples.

Describe an algorithm with O(n^2) complexity and provide an example.

O(n^2) or quadratic time complexity means the execution time grows proportionally to the square of the input size, typically involving nested iterations. A common example is selection sort or iterating through all pairs in a single array.

Describe an algorithm with O(2^n) complexity and provide an example.

O(2^n) or exponential time complexity means the execution time doubles with each additional unit of input, indicating a very inefficient algorithm for larger inputs. Calculating the nth Fibonacci number using a naive recursive approach is an example.

What is the rule for handling constant factors in Big O Notation?

Constant factors are dropped because Big O Notation focuses on the growth rate as input size 'n' approaches infinity, where constants become insignificant. For example, O(2n) simplifies to O(n).

What is the rule for handling non-dominant terms in Big O Notation?

Non-dominant terms are dropped because they contribute less to the overall growth rate as 'n' becomes very large. For instance, O(n^2 + n) simplifies to O(n^2), as n^2 dominates n.

What is the Big O complexity for a single 'for' loop that iterates 'n' times?

The Big O complexity is O(n), as the number of operations inside the loop is executed 'n' times, making the execution time directly proportional to the input size.

What is the Big O complexity of the binary search algorithm?

The Big O complexity of binary search is O(log n) because it repeatedly divides the search space in half until the target element is found or the space is exhausted.

What is the Big O complexity for an algorithm with two nested 'for' loops, where each loop iterates 'n' times?

The Big O complexity is O(n^2) because for each iteration of the outer loop, the inner loop runs 'n' times, resulting in n * n operations in total.