Q. In a queue, what is the time complexity of dequeuing an element?
-
A.
O(1)
-
B.
O(n)
-
C.
O(log n)
-
D.
O(n^2)
Solution
Dequeuing an element from a queue is done in constant time, O(1), when implemented using a linked list.
Correct Answer:
A
— O(1)
Learn More →
Q. In a stack, what is the time complexity of accessing the top element?
-
A.
O(1)
-
B.
O(n)
-
C.
O(log n)
-
D.
O(n^2)
Solution
Accessing the top element of a stack is done in constant time, O(1), as it simply involves returning the last added element.
Correct Answer:
A
— O(1)
Learn More →
Q. What is the space complexity of a queue implemented using two stacks?
-
A.
O(1)
-
B.
O(n)
-
C.
O(log n)
-
D.
O(n^2)
Solution
The space complexity of a queue implemented using two stacks is O(n) as it requires space for all elements in the queue.
Correct Answer:
B
— O(n)
Learn More →
Q. What is the time complexity of checking if a stack is empty?
-
A.
O(1)
-
B.
O(n)
-
C.
O(log n)
-
D.
O(n^2)
Solution
Checking if a stack is empty is done in constant time, O(1), by checking if the size is zero.
Correct Answer:
A
— O(1)
Learn More →
Q. What is the time complexity of reversing a stack using recursion?
-
A.
O(n)
-
B.
O(n log n)
-
C.
O(n^2)
-
D.
O(1)
Solution
Reversing a stack using recursion has a time complexity of O(n) as each element is processed once.
Correct Answer:
A
— O(n)
Learn More →
Q. What is the worst-case time complexity of sorting a stack using another stack?
-
A.
O(n)
-
B.
O(n log n)
-
C.
O(n^2)
-
D.
O(n^3)
Solution
The worst-case time complexity of sorting a stack using another stack is O(n^2) due to the need to repeatedly pop and push elements.
Correct Answer:
C
— O(n^2)
Learn More →
Showing 1 to 6 of 6 (1 Pages)