Quiz on Complexity analysis for DSA

Last Updated :
Discuss
Comments

Question 1

Which of the following best represents the time complexity of accessing an element in an array by its index?

  • O(1)

  • O(n)

  • O(log n)

  • O(n^2)

Question 2

What is the time complexity of a nested loop where the outer loop runs n times and the inner loop runs m times?

  • O(n)

  • O(m)

  • O(n * m)

  • O(n + m)

Question 3

Consider an algorithm that takes an array of size n and performs a loop from 1 to n^2. What is the time complexity of this algorithm?

  • O(n)

  • O(n2)

  • O(n3)

  • O(n4)

Question 4

Which of the following is true about the Big O notation (O(f(n)))?

  • It describes the lower bound of the runtime of an algorithm.

  • It describes the exact runtime of an algorithm.

  • It describes the upper bound of the runtime of an algorithm.

  • It describes the average runtime of an algorithm.

Question 5

Which of the following options correctly matches the notation with the parameters they define

  • Big-O Notation (O-notation) : Average Case complexity

    Omega Notation (Ω-notation) : Worst Case complexity

    Theta Notation (Θ-notation) : Best Case complexity

  • Big-O Notation (O-notation) : Best Case complexity

    Omega Notation (Ω-notation) : Average Case complexity

    Theta Notation (Θ-notation) : Worst Case complexity

  • Big-O Notation (O-notation) : Worst Case complexity

    Omega Notation (Ω-notation) : Average Case complexity

    Theta Notation (Θ-notation) : Best Case complexity

  • Big-O Notation (O-notation) : Worst Case complexity

    Omega Notation (Ω-notation) : Best Case complexity

    Theta Notation (Θ-notation) : Average Case complexity

Question 6

If an algorithm’s time complexity is O(n^2), which of the following best describes its performance when the input size is doubled?

  • The time taken will double.

  • The time taken will remain the same.

  • The time taken will increase by a factor of 2^2.

  • The time taken will quadruple.

Question 7

What does O(n) time complexity indicate in an algorithm?

  • The algorithm's execution time grows exponentially with the input size.

  • The algorithm's execution time grows linearly with the input size.

  • The algorithm’s execution time does not depend on the input size.

  • The algorithm’s execution time is constant, irrespective of the input size.

Question 8

Determine the time complexity for the following recursive function :

C++
int recursive(n)
{
    if (n <= 1) return 1;
    else 
    {
        return recursive(n - 1) + recursive(n - 1);
    }
    
}
C
int recursive(n)
{
    if (n <= 1) return 1;
    else 
    {
        return recursive(n - 1) + recursive(n - 1);
    }
    
}
Java
public static int recursive(int n) {
        if (n <= 1) {
            return 1;
        } else {
            return recursive(n - 1) + recursive(n - 1);
        }
    }
Python
def recursive(n):
    if n <= 1:
        return 1
    else:
        return recursive(n - 1) + recursive(n - 1)
JavaScript
function recursive(n) {
    if (n <= 1) {
        return 1;
    } else {
        return recursive(n - 1) + recursive(n - 1);
    }
}


  • O(n)

  • O(log n)

  • O(2^n)

  • O(n^2)

Question 9

What is the time complexity of the following recursive function?
Note : The merge function takes linear time.

C++
vector<int> mergeSort(const vector<int>& arr) {
    if (arr.size() <= 1) {
        return arr;
    }
    int mid = arr.size() / 2;
    vector<int> left = mergeSort(vector<int>(arr.begin(), arr.begin() + mid));
    vector<int> right = mergeSort(vector<int>(arr.begin() + mid, arr.end()));

    return merge(left, right);
}
C
void mergeSort(int arr[], int size) {
    if (size <= 1) {
        return;
    }

    int mid = size / 2;
    int* left = (int*)malloc(mid * sizeof(int));
    int* right = (int*)malloc((size - mid) * sizeof(int));

    for (int i = 0; i < mid; i++) {
        left[i] = arr[i];
    }
    for (int i = mid; i < size; i++) {
        right[i - mid] = arr[i];
    }

    mergeSort(left, mid);
    mergeSort(right, size - mid);
    merge(arr, left, mid, right, size - mid);
}
Java
public static int[] mergeSort(int[] arr) {
        if (arr.length <= 1) {
            return arr;
        }
        int mid = arr.length / 2;
        int[] left = mergeSort(Arrays.copyOfRange(arr, 0, mid));
        int[] right = mergeSort(Arrays.copyOfRange(arr, mid, arr.length));

        return merge(left, right);
    }
Python
def merge_sort(arr):
    if len(arr) <= 1:
        return arr
    mid = len(arr) // 2
    left = merge_sort(arr[:mid])
    right = merge_sort(arr[mid:])
    
    return merge(left, right)
JavaScript
function mergeSort(arr) {
    if (arr.length <= 1) {
        return arr;
    }
    const mid = Math.floor(arr.length / 2);
    const left = mergeSort(arr.slice(0, mid));
    const right = mergeSort(arr.slice(mid));

    return merge(left, right);
}


  • O(log n)

  • O(n log n)

  • O(n^2)

  • O(n)

Question 10

Consider a recursive function with a branching factor of 3 and a depth of recursion of n. What would be the time complexity of this recursion?

  • O(3n)

  • O(n3)

  • O(3n)

  • O(n2)

Tags:

There are 17 questions to complete.

Take a part in the ongoing discussion