Test on time complexity

Time complexity is used for analyzing sorting functions, recursive calculations and things which generally take more computing time. This quiz will test you on this knowledge of calculating time complexity of algorithms.

8 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

What is time complexity of fun()?

int fun(int n)
{
  int count = 0;
  for (int i = 0; i < n; i++)
     for (int j = i; j > 0; j--)
        count = count + 1;
  return count;
}
  1. $\theta(n^2)$
  2. $\theta(nlogn)$
  3. $\theta(n)$
  4. $\theta(logn)^2$
Question 2 Multiple Choice (Single Answer)

Let w(n) and A(n) denote respectively, the worst case and average case running time of an algorithm executed on an input of size n. which of the following is ALWAYS TRUE?

  1. A(n) = $\Omega$ W(n)
  2. A(n) = $O$ W(n)
  3. A(n) = $\Theta$ W(n)
  4. A(n) = o W(n)
Question 3 Multiple Choice (Single Answer)

What is time complexity for the following function?

public void printAllPossibleOrderedPairs ( int[] arrayOfItems)
{  
   for (int firstItem : arrayOfItems)
   { 
       for (int secondItem : arrayOfItems)
       {
           int[] orderedPair = new int[] { firstItem, secondItem};
           system.out.print( Arrays.toString(orderedPair));
       }
   }
}
  1. $O (n)$
  2. $O (n logn)$
  3. $O (n^2)$
  4. $O (logn)^2$
Question 4 Multiple Choice (Single Answer)

Consider the following two functions. What are time complexities of the functions?

int fun1(int n)
{
    if (n <= 1) return n;
    return 2*fun1(n-1);
}

int fun2(int n)
{
    if (n <= 1) return n;
    return fun2(n-1) + fun2(n-1);
}
  1. $O(2^n)$ for both fun1() and fun2()
  2. $O(n)$ for fun1() and $O(2^n)$ for fun2()
  3. $O(2^n)$ for fun1() and $O(n)$ for fun2()
  4. $O(n)$ for both fun1() and fun2()
Question 5 Multiple Choice (Single Answer)

What is not true about insertion sort?

  1. Exhibits the worst case performance when the initial array is sorted in reverse order.
  2. Worst case and average case performance is Ο(n2)
  3. Can be compared to the way a card player arranges his card from a card deck.
  4. None of the above!
Question 6 Multiple Choice (Single Answer)

If queue is implemented using arrays, what would be the worst run time complexity of queue and dequeue operations?

  1. $O(n), O(1)$
  2. $O(1), O(1)$
  3. $O(1), O(n)$
  4. $O(n), \theta(1)$
Question 7 Multiple Choice (Single Answer)

What is the worst case run-time complexity of binary search algorithm?

  1. $Ο(n^2)$
  2. $O(log_2n)$
  3. $O(n logn)$
  4. $O(log logn)$
Question 8 Multiple Choice (Single Answer)

The number of passes needed to sort the numbers 8, 22, 7, 9, 31 in ascending order, using insertion sort is

  1. 5
  2. 4
  3. 6
  4. 3