Big O Notation Flashcards

1
Q

What are the types of Big O Notation

A

Big O notation classifies the growth rate of algorithms in terms of their worst-case time complexity. Here are some common types of Big O notations:

  1. O(1) - Constant Time: The algorithm’s runtime remains constant regardless of input size.
  2. O(log n) - Logarithmic Time: The runtime grows logarithmically as the input size increases.
  3. O(n) - Linear Time: The runtime grows linearly with the input size.
  4. O(n log n) - Linearithmic Time: Common in more efficient sorting algorithms like Merge Sort and Heap Sort.
  5. O(n^2) - Quadratic Time: The runtime grows quadratically with the input size.
  6. O(n^c) - Polynomial Time: The runtime grows with the input size raised to a constant exponent c.
  7. O(2^n) - Exponential Time: The runtime doubles with each additional element in the input.
  8. O(n!) - Factorial Time: The runtime grows factorially with the input size.

These are just a few examples of Big O notations, and there are other variations and complexities as well. The goal is to choose algorithms and data structures that minimize these complexities to optimize performance for a given problem.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is Big O Notation

A

Big O notation is a mathematical notation used in computer science to describe the upper bound or worst-case time complexity of an algorithm in terms of its input size. It provides a way to analyze and compare the efficiency of different algorithms based on how they perform as the input size grows.

In simpler terms, Big O notation helps us understand how the runtime of an algorithm increases relative to the size of the input data. It expresses the scalability of an algorithm, allowing developers to make informed decisions about which algorithm to use for a particular problem based on its efficiency.

For example, an algorithm with a time complexity of O(1) means that its runtime is constant and doesn’t change with the input size. On the other hand, an algorithm with a time complexity of O(n^2) means that its runtime grows quadratically as the input size increases.

By using Big O notation, developers can make better decisions about optimizing algorithms, choosing appropriate data structures, and predicting how an algorithm will perform when faced with larger datasets.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the Big O for linked list operations

A

The Big O notation for common operations on linked lists is as follows:

  1. Access/Search: O(n)
  2. Insertion at the beginning: O(1)
  3. Insertion at the end: O(n) (unless maintaining a tail pointer)
  4. Insertion in the middle: O(n)
  5. Deletion at the beginning: O(1)
  6. Deletion at the end: O(n)
  7. Deletion in the middle: O(n)

Note that these complexities can vary depending on the specific implementation and whether the linked list is singly or doubly linked.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the Big O Notations for ArrayList functions?

A

Here are the Big O notations for common operations on ArrayLists (dynamic arrays):

  1. Access/Search by Index: O(1)
  2. Insertion at the End (Amortized): O(1)
  3. Insertion at a Specific Index: O(n) (May require shifting elements)
  4. Deletion at the End: O(1)
  5. Deletion at a Specific Index: O(n) (May require shifting elements)
  6. Searching for an Element: O(n)
  7. Appending an Element (when resizing needed): O(n) - can be amortized to O(1)
  8. Resizing (Growing/Shrinking): O(n) - happens infrequently
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are is the Big O for Hash Table Functions?

A
  1. Insertion: O(1) - On average, insertions take constant time due to the efficient distribution of keys and handling of collisions.
    1. Deletion: O(1) - Similar to insertions, deletions also take constant time on average.
    2. Access/Search: O(1) - Retrieving a value associated with a given key is typically constant time due to the efficient indexing of hash tables.
    3. Worst-Case Access/Search: O(n) - In the worst case, due to hash collisions, the access time might degrade to linear time, but this is rare with a good hash function.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the Big O notations for common operations on Arrays

A
  1. Access/Search by Index: O(1)
    1. Insertion/Deletion at the End: O(1) (if space is available)
    2. Insertion/Deletion at a Specific Index: O(n) (because shifting may be required)
    3. Searching for an Element: O(n)
    4. Appending an Element: O(1) (if space is available)
    5. Resizing (Growing): O(n) - since it involves copying elements to a new array
    6. Resizing (Shrinking): O(n) - same as growing, involves copying elements

Keep in mind that these complexities are worst-case scenarios and might vary depending on specific implementations or optimizations. Arrays have constant-time access by index, but insertion, deletion, and resizing operations can be costly due to the potential need to shift elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the Big O notations for common operations on binary trees

A
  1. Access/Search: O(log n) for balanced binary search trees (like AVL or Red-Black trees). However, in worst cases, for unbalanced trees, it can be O(n).
  2. Insertion: O(log n) for balanced binary search trees on average. In worst cases, for unbalanced trees, it can be O(n) due to tree rebalancing.
  3. Deletion: O(log n) for balanced binary search trees on average. In worst cases, for unbalanced trees, it can be O(n) due to tree rebalancing.
  4. Traversals (In-order, Pre-order, Post-order): O(n) - Each node is visited exactly once.
  5. Finding Maximum/Minimum: O(log n) for balanced binary search trees, O(n) for unbalanced trees.
  6. Breadth-First Search (Level Order Traversal): O(n) - All nodes are visited.
  7. Height/Depth Calculation: O(n) - The height of the tree needs to be calculated by visiting all nodes in the worst case.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly