Question 31
MediumWhat is the output of this code?
from collections import deque
d = deque()
for x [, , ]:
d.appendleft(x)
((d))
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the output of this code?
from collections import deque
d = deque()
for x [, , ]:
d.appendleft(x)
((d))
[1, 3, 2]
[1, 2, 3]
[3, 2, 1]
[2, 3, 1]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
from collections import deque
d = deque([1, 2], maxlen=2)
d.append(3)
print(list(d))
[1, 2, 3]
[3]
Sign up free to see the answer and a step-by-step explanation.
How can a stack be implemented with queues?
With one queue: after each enqueue, rotate the earlier elements behind the new one
It is impossible
By sorting the queue
With a single dequeue operation
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of enqueue and dequeue in a queue implemented with a linked list (with head and tail pointers)?
O(1) for both
O(n) for both
O(1) enqueue, O(n) dequeue
O(log n) for both
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
from collections import deque
q = deque([1, 2, 3, 4, 5])
k = 3
s = list(q)[:k][::-1]
q = deque(s + list(q)[k:])
q.rotate(-1)
print(list(q))
[1, 4, 5, 3, 2]
[2, 3, 4, 5, 1]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
from collections import deque
q = deque(["P1", "P2", "P3"])
left = {"P1": 5, "P2": 2, "P3": 4}
order = []
while q:
p = q.popleft()
left[p] -= 2
if left[p] > 0:
q.append(p)
else:
order.append(p)
print(order)
['P2', 'P3', 'P1']
['P3', 'P2', 'P1']
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
from collections import deque
q = deque()
for x in [10, 20, 30]:
q.append(x)
q.popleft()
q.appendleft(5)
q.append(40)
print(q[0], q[-1], len(q))
20 40 3
10 40 4
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
from collections import deque
def josephus(n, k):
q = deque(range(1, n + 1))
while len(q) > 1:
q.rotate(-(k - 1))
q.popleft()
return q[0]
print(josephus(7, 3))
7
1
Sign up free to see the answer and a step-by-step explanation.
A circular queue of capacity 6 has front = 4 and rear = 1 (both indexes inclusive). How many elements does it hold?
5
4
3
2
Sign up free to see the answer and a step-by-step explanation.
In a circular queue of capacity n using the 'one empty slot' rule, when is it full?
When (rear + 1) mod n == front
When front == 0
When rear == n − 1
When rear == front
Sign up free to see the answer and a step-by-step explanation.
[1, 2]
[2, 3]
[3, 2, 1, 4, 5]
[2, 1, 4, 5, 3]
['P1', 'P2', 'P3']
['P2', 'P1', 'P3']
5 30 3
5 40 4
4
3