quiz

Space Complexity of Algorithms

1. What does space complexity in algorithms include?

  • A) Input memory space
  • B) Output memory space
  • C) Both input space and auxiliary memory space
  • D) The memory space used by the algorithm's variables
C) Both input space and auxiliary memory space Explanation

2. Which of the following indicates the most efficient use of memory in terms of input size n?

  • A) O(1)
  • B) O(logn)
  • C) O(n)
  • D) O(n)
A) O(1) Explanation

3. What is the space complexity of an algorithm for storing elements in an array of size n?

  • A) O(n)
  • B) 𝑂(1)
  • C) O(logn)
  • D) O(n2 )
A) O(n) Explanation

4. What is the space and time complexity of the following code?

void example(int n) { int i, j; for (i = 0; i < n; i++) { printf("%d ", i); } for (j = 0; j < n; j++) { printf("%d ", j); } }
  • A) Time Complexity: O(n), Space Complexity: O(n)
  • B) Time Complexity: O(n), Space Complexity: O(1)
  • C) Time complexity: O(n 2), Space complexity: O(1)
  • D) Time complexity is O(1), but space complexity is O(n)
B) Time Complexity: O(n), Space Complexity: O(1) Explanation