What is the purpose of the following function?
def fun(mydeque):
if not (isEmpty()):
return mydeque[0]
else:
print("empty")
What is the purpose of the following function?
def fun(mydeque):
if not (isEmpty()):
return mydeque[0]
else:
print("empty")
Solution
The function first checks that the deque is not empty. It then returns mydeque[0], which is the element at the front, and does not change the deque. If the deque is empty, it prints "empty". This is the peek (getFront) operation: it reads the front value without removing it.
Options 1 and 3: insertion needs append() or appendleft()-style operations that change the deque, but this function only reads an index.
Option 2: deletion needs a removal such as pop() or del, and mydeque[0] is the front, not the rear (the rear is mydeque[-1]).
Related questions:
2025: 27 May Shift 2
2026: 11 May Shift 2
2026: 18 May Shift 2