Data Structures & AlgorithmsModerateProduct placement · Amazon/Microsoft
A stack is implemented with two queues where push is costly (new element goes into the empty queue, then everything else is moved behind it). What are the costs of push and pop for n elements?
- A. push O(1), pop O(1)
- B. push O(log n), pop O(log n)
- C. push O(n), pop O(1)
- D. push O(1), pop O(n)
Correct answer: C. push O(n), pop O(1)
Each push reorders the queue so the newest element is at the front, which moves all n existing elements; pop is then a single dequeue. The alternative design makes pop costly instead.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Daily prices are [15, 17, 19, 13, 20, 17, 38, 38, 40]. What is the maximum profit with at most ONE buy and one later sell, and with…
- Using a sliding window with a last-seen index map, what is the length of the longest substring of "dbdcccdcbaddad" with all characters…
- Two strings are isomorphic if characters can be replaced one-to-one to turn one into the other. Version A checks both directions; version…
- Swap every two adjacent nodes of a linked list by relinking (not by swapping values). What is printed? python def swappairs(h): d = p =…
- Count distinct values in every window of size k with a frequency map. What is printed? python from collections import Counter a, k = [3…
- A min-stack keeps a second stack of running minimums. What is printed? python st, mn, out = [], [], [] for op in [7, 5, 7, 5, 5, "pop"…