Q. In a stack, what is the time complexity of checking if it 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), as it only requires checking the size or the top pointer.
Correct Answer:
A
— O(1)
Learn More →
Q. What is the time complexity of clearing all elements from a queue?
-
A.
O(1)
-
B.
O(n)
-
C.
O(log n)
-
D.
O(n^2)
Solution
Clearing all elements from a queue takes O(n) time, as each element must be removed individually.
Correct Answer:
B
— O(n)
Learn More →
Q. What is the time complexity of dequeuing an element from a queue implemented using an array?
-
A.
O(1)
-
B.
O(n)
-
C.
O(log n)
-
D.
O(n^2)
Solution
Dequeuing an element from a queue implemented using an array can take O(n) time if all elements need to be shifted after removing the front element.
Correct Answer:
B
— O(n)
Learn More →
Q. What is the time complexity of enqueuing an element in a queue implemented using an array?
-
A.
O(1)
-
B.
O(n)
-
C.
O(log n)
-
D.
O(n^2)
Solution
Enqueuing an element in a queue implemented using an array is done in constant time, O(1), unless the array needs to be resized.
Correct Answer:
A
— O(1)
Learn More →
Q. What is the time complexity of implementing a queue using two stacks?
-
A.
O(1) for enqueue, O(n) for dequeue
-
B.
O(n) for enqueue, O(1) for dequeue
-
C.
O(n) for both
-
D.
O(1) for both
Solution
Implementing a queue using two stacks allows O(1) time for enqueue and O(n) time for dequeue, as elements may need to be transferred between stacks.
Correct Answer:
A
— O(1) for enqueue, O(n) for dequeue
Learn More →
Q. What is the time complexity of reversing a stack using another stack?
-
A.
O(n)
-
B.
O(n log n)
-
C.
O(n^2)
-
D.
O(1)
Solution
Reversing a stack using another stack takes O(n) time, as each element is pushed and popped once.
Correct Answer:
A
— O(n)
Learn More →
Q. What is the time complexity of searching for an element in a stack?
-
A.
O(1)
-
B.
O(n)
-
C.
O(log n)
-
D.
O(n^2)
Solution
Searching for an element in a stack takes O(n) time in the worst case, as you may need to traverse all elements.
Correct Answer:
B
— O(n)
Learn More →
Showing 1 to 7 of 7 (1 Pages)