Python Deque

Last Updated :
Discuss
Comments

Question 1

What is the primary advantage of using a deque over a traditional list in Python?

  • A

    Deque allows random access to elements.

  • B

    Deque supports efficient append and pop operations from both ends.

  • C

    Deque automatically sorts elements upon insertion.

  • D

    Deque has a fixed size and cannot grow dynamically.

Question 2

In a double-ended queue (deque), which operation is inefficient compared to lists?

  • A

    Appending elements to both ends.

  • B

    Popping elements from both ends.

  • C

    Random access to elements by index.

  • D

    Removing elements from both ends.

Question 3

What is the time complexity of the remove(value) operation in a deque?

  • A

    O(1)

  • B

    O(n)

  • C

    O(k)

  • D

    O(log n)

Question 4

Which of the following best describes an Input Restricted Deque?

  • A

    Elements can be added from both ends, but removed only from one end.

  • B

    Elements can be added and removed from both ends.

  • C

    Elements can only be added from one end and removed from both ends.

  • D

    Elements can be added and removed only from the right end.

Question 5

When using the rotate(n) method on a deque, what does a negative value for n signify?

  • A

    Rotate to the left by n steps.

  • B

    Rotate to the right by n steps.

  • C

    Reverse the deque entirely.

  • D

    No rotation occurs.

Question 6

Which method can be used to append an element to the right side of a deque?

  • A

    insert()

  • B

    appendleft()

  • C

    append()

  • D

    rightappend()

Question 7

Which of the following is a valid way to initialize a deque in Python?

  • A

    d = deque([1, 2, 3, 4])

  • B

    d = deque("hello")

  • C

    d = deque()

  • D

    All of the above

Question 8

What will be the output of the following code?

Python
from collections import deque
d = deque([1, 2, 3, 4])
d.rotate(1)
print(d)
  • A

    [2, 3, 4, 1]

  • B

    [4, 1, 2, 3]

  • C

    [1, 2, 3, 4]

  • D

    [3, 4, 1, 2]

Question 9

Which of the following deque methods can be used to remove an element from the right end of the deque?

  • A

    pop()

  • B

    remove()

  • C

    popright()

  • D

    removeleft()

Question 10

What will be the output of the following code?

Python
from collections import deque
d = deque([1, 2, 3, 4])
d.extendleft([0, -1])
print(d)
  • A

    [-1, 0, 1, 2, 3, 4]

  • B

    [0, -1, 1, 2, 3, 4]

  • C

    [1, 2, 3, 4, 0, -1]

  • D

    [0, 1, 2, 3, 4, -1]

There are 10 questions to complete.

Take a part in the ongoing discussion