A double‐ended queue (dequeue) supports adding and removing items from both the ends of the queue. The operations supported by dequeue are AddFront(adding item to front of the queue), AddRear(adding item to the rear of the queue), RemoveFront(removing item from the front of the queue), and RemoveRear(removing item from the rear of the queue). You are given only stacks to implement this data structure. You can implement only push and pop operations. What’s the time complexity of performing AddFront() and AddRear() n assuming m is the size of the stack and is the number of elements?
Pick an option to check yourself, or open the answer below.
Show answer & explanation
Correct answer: B. O(1) and O(n)
Explanation
Use one stack whose top is the front. AddFront is a single push, O(1). AddRear must move all n elements to another stack, push, and move them back: O(n).
More Data Structures and Algorithms questions
- Two balanced binary trees are given with m and n elements, respectively. They can be merged into a balanced binary search tree in ____ time.
- A data structure is required for storing a set of integers such that each of the following O(log n) operations can be done in time, where n…
- Consider the following graph. Among the following sequences I. a b e g h f II. a b f e h g III. a b f h g e IV. a f g h b e Which are depth…
- Which of the given options provides the increasing order of asymptotic complexity of f1, f2, f3 f4 functions and ? f1(n) = 2n A. f2(n) = n3…
- A hash function h(key) = key mod 7 with linear probing is used to insert the keys 44, 45, 79, 55, 91, 18, 63 into a table indexed 0 to 6. W…
- Match each graph algorithm with its time complexity (n vertices, m edges). List I: A. Bellman-Ford, B. Kruskal's algorithm, C. Floyd-Warsha…