Conceptual Questions
Q1. Which of the following best describes time complexity?
a) Amount of memory required by an algorithm
b) Number of primitive operations executed by an algorithm
c) Number of variables used in an algorithm
d) Number of recursive calls in an algorithm
Answer: b
Q2. What is the time complexity of linear search in an unsorted array of size n?
a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
Answer: c
Q3. Which of the following algorithms has O(log n) time complexity in the best, average, and worst
case?
a) Merge Sort
b) Binary Search
c) Linear Search
d) Selection Sort
Answer: b
Q4. What is the space complexity of recursive Fibonacci implementation?
a) O(1)
b) O(n)
c) O(n²)
d) O(log n)
Answer: b (stack space for recursion depth)
Q5. Which of the following sorting algorithms is not stable?
a) Merge Sort
b) Quick Sort
c) Bubble Sort
d) Insertion Sort
Answer: b
Code-Based Questions
Q6. What will be the time complexity of the following code?
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << i << " " << j << endl;
a) O(n)
b) O(n log n)
c) O(n²)
d) O(2n)
Answer: c
Q7. Consider the following code:
int fun(int n) {
if (n <= 1) return 1;
return fun(n-1) + fun(n-1);
What is the time complexity of this function?
a) O(n)
b) O(2^n)
c) O(n²)
d) O(log n)
Answer: b
Q8. What is the output of the following code when n = 5?
int count = 0;
for (int i = 1; i < n; i = i * 2) {
count++;
cout << count;
a) 2
b) 3
c) 4
d) 5
Answer: b (loop runs for i = 1, 2, 4 → 3 times)
Q9. What is the time complexity of the following code? for
(int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << i << " " << j << endl;
a) O(n)
b) O(n²)
c) O(n log n)
d) O(2^n)
Answer: b (inner loop runs ~n(n+1)/2 times)
Q10. What is the space complexity of the following function? int
factorial(int n) {
if (n == 0) return 1; return
n * factorial(n-1);
a) O(1)
b) O(n)
c) O(n²)
d) O(log n)
Answer: b (recursion stack of depth n)
Q11. Which of the following has the best average-case time complexity among these sorting
algorithms?
a) Bubble Sort
b) Merge Sort
c) Insertion Sort
d) Selection Sort
Answer: b
Q12. If an algorithm’s time complexity is O(n log n), which of the following algorithms might it
represent?
a) Bubble Sort
b) Merge Sort
c) Binary Search
d) Linear Search
Answer: b
Q13. What is the space complexity of iterative binary search?
a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: a
Q14. Which data structure is most suitable for implementing Breadth First Search (BFS) in a graph?
a) Stack
b) Queue
c) Priority Queue
d) Linked List
Answer: b
Q15. What will be the time complexity of the following code?
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++)
{ cout << i + j + k;
a) O(n²)
b) O(n³)
c) O(n log n)
d) O(2^n)
Answer: b
Q16. Which of the following algorithms is in-place (does not require extra space)?
a) Merge Sort
b) Heap Sort
c) Radix Sort
d) Counting Sort
Answer: b
Q17. Consider the function:
void fun(int n) {
if (n <= 1) return;
for (int i = 0; i < n; i++)
{ cout << i;
fun(n/2);
What is its time complexity?
a) O(n)
b) O(n log n)
c) O(n²)
d) O(log n)
Answer: b
Q18. Which of the following complexities grows the slowest as n increases?
a) O(n)
b) O(log n)
c) O(n log n)
d) O(√n)
Answer: b
Q19. What is the output of the following code for n = 8?
int count = 0;
for (int i = n; i > 0; i = i / 2) {
count++;
cout << count;
a) 2
b) 3
c) 4
d) 8
Answer: c (loop runs log₂(8) = 3 times, but includes i=1 → 4 times)
Q20. The time complexity of Quick Sort in the worst case is:
a) O(n log n)
b) O(n²)
c) O(n)
d) O(log n)
Answer: b