Time Complexity Flashcards

Big O notaqtion

1
Q

What is the time complexity of this function?

public static void printFirstItem(int[] items) {
System.out.println(items[0]);
}

A

This method runs in O(1)O(1) time (or “constant time”) relative to its input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
void printAllItems(const vector& items)
{
    for (int item : items) {
        cout << item << endl;
    }
}
A

This function runs in O(n) time (or “linear time”), where “n” is the number of items in the vector.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
void printAllPossibleOrderedPairs(const vector&amp; items)
{
    for (int firstItem : items) {
        for (int secondItem : items) {
            cout << firstItem << ", " << secondItem << endl;
        }
    }
}
A

this function runs in O(n^2) time (or “quadratic time”).

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

Average and Worst Time Complexity to Access an Array

A

Average : O(1)

Worst : O(1)

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