Question 1
EasyWhich principle does a queue follow?
- A.
LIFO (last in, first out)
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
Which principle does a queue follow?
LIFO (last in, first out)
Priority only
Random order
FIFO (first in, first out)
Answer: D
Like a line at a ticket counter, the first to arrive is served first.
Which operation adds an element to a queue?
push to the top
enqueue
dequeue
pop
Answer: B
enqueue adds at the rear; dequeue removes from the front.
What is the output of this code?
from collections import deque
q = deque()
q.append(1)
q.append(2)
q.append(3)
print(q.popleft())
[2, 3]
2
Answer: C
popleft() removes the element at the front, the first one added.
Which graph traversal uses a queue?
Backtracking
Depth-first search
Inorder traversal
Breadth-first search
Answer: D
BFS visits nodes level by level, in the order they were discovered.
What is a deque?
A queue of size 1
A queue that only removes
A double-ended queue that allows insertion and removal at both ends
A sorted queue
Answer: C
Python's collections.deque gives O(1) appends and pops at both ends.
Why is list.pop(0) slow for a Python list used as a queue?
Lists can't hold numbers
Removing the first element shifts all the others: O(n)
It removes the last element
It isn't allowed
Answer: B
Use collections.deque, whose popleft() is O(1).
Which real-world system is modelled by a queue?
A browser's back button
The undo history of an editor
Function calls in recursion
Print jobs waiting for a printer
Answer: D
Jobs are printed in the order they were sent.
Which operation removes the element at the front of a queue?
enqueue
peek
push
dequeue
Answer: D
Elements leave from the front in the order they arrived.
Starting with an empty queue, you enqueue 1, 2 and 3 and then dequeue once. Which element is at the front now?
3
The queue is empty
1
2
Answer: D
First in, first out: 1 leaves, so 2 is now at the front.
At which end of a queue are new elements added?
Either end at random
The rear (back)
The front
The middle
Answer: B
Elements join at the rear and leave from the front.
1
3