Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Queue Implementation Using Array in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Queue Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after these queue operations?

Consider a queue implemented using an array with a fixed size of 5. Initially, the queue is empty.

Operations performed in order:

  1. enqueue(10)
  2. enqueue(20)
  3. dequeue()
  4. enqueue(30)
  5. enqueue(40)
  6. dequeue()
  7. enqueue(50)

What is the state of the queue from front to rear?

DSA Python
class Queue:
    def __init__(self, size=5):
        self.size = size
        self.queue = [None]*size
        self.front = -1
        self.rear = -1

    def enqueue(self, val):
        if self.rear == self.size - 1:
            return "Queue Full"
        else:
            if self.front == -1:
                self.front = 0
            self.rear += 1
            self.queue[self.rear] = val

    def dequeue(self):
        if self.front == -1 or self.front > self.rear:
            return "Queue Empty"
        else:
            val = self.queue[self.front]
            self.front += 1
            return val

    def display(self):
        if self.front == -1 or self.front > self.rear:
            return []
        return self.queue[self.front:self.rear+1]

q = Queue()
q.enqueue(10)
q.enqueue(20)
q.dequeue()
q.enqueue(30)
q.enqueue(40)
dq = q.dequeue()
q.enqueue(50)
print(q.display())
A[30, 40, 50]
B[20, 30, 40, 50]
C[40, 50]
D[10, 20, 30, 40, 50]
Attempts:
2 left
💡 Hint

Remember that dequeue removes from the front and enqueue adds at the rear. The queue does not shift elements after dequeue.

🧠 Conceptual
intermediate
1:00remaining
What happens when you enqueue in a full queue implemented with an array?

In a fixed-size array queue implementation, what is the typical behavior when you try to enqueue an element but the queue is already full?

AThe oldest element is removed to make space for the new element.
BThe queue automatically resizes to accommodate the new element.
CThe enqueue operation fails or returns an error indicating the queue is full.
DThe new element overwrites the last element in the queue.
Attempts:
2 left
💡 Hint

Think about fixed size and no dynamic resizing.

🔧 Debug
advanced
1:30remaining
Identify the error in this dequeue method of array queue

What error will this dequeue method cause when called repeatedly on a queue?

DSA Python
def dequeue(self):
    if self.front == -1 or self.front > self.rear:
        return "Queue Empty"
    val = self.queue[self.front]
    self.front += 1
    if self.front > self.rear:
        self.front = self.rear = -1
    return val
ANo error; works correctly resetting front and rear when empty.
BAfter all elements are dequeued, enqueue will fail because front and rear are not reset.
CIt causes an IndexError when front exceeds rear because it accesses invalid index.
DIt causes infinite loop because front is never incremented.
Attempts:
2 left
💡 Hint

Check how front and rear are reset after last element is dequeued.

❓ Predict Output
advanced
2:00remaining
What is the output of this circular queue enqueue sequence?

Given a circular queue of size 4, initially empty, perform these operations:

  1. enqueue(1)
  2. enqueue(2)
  3. enqueue(3)
  4. dequeue()
  5. enqueue(4)
  6. enqueue(5)

What is the queue content from front to rear?

DSA Python
class CircularQueue:
    def __init__(self, size=4):
        self.size = size
        self.queue = [None]*size
        self.front = -1
        self.rear = -1

    def enqueue(self, val):
        if (self.rear + 1) % self.size == self.front:
            return "Queue Full"
        if self.front == -1:
            self.front = 0
        self.rear = (self.rear + 1) % self.size
        self.queue[self.rear] = val

    def dequeue(self):
        if self.front == -1:
            return "Queue Empty"
        val = self.queue[self.front]
        if self.front == self.rear:
            self.front = self.rear = -1
        else:
            self.front = (self.front + 1) % self.size
        return val

    def display(self):
        if self.front == -1:
            return []
        result = []
        i = self.front
        while True:
            result.append(self.queue[i])
            if i == self.rear:
                break
            i = (i + 1) % self.size
        return result

cq = CircularQueue()
cq.enqueue(1)
cq.enqueue(2)
cq.enqueue(3)
cq.dequeue()
cq.enqueue(4)
cq.enqueue(5)
print(cq.display())
A[2, 3, 4, 5]
B[3, 4, 5]
C[1, 2, 3, 4]
D[4, 5]
Attempts:
2 left
💡 Hint

Remember circular queue wraps around the array.

🧠 Conceptual
expert
1:30remaining
Why is a circular queue preferred over a simple array queue?

Which of the following best explains why circular queue implementation is preferred over a simple array queue?

ACircular queue stores elements in sorted order automatically.
BCircular queue allows dynamic resizing of the array automatically.
CCircular queue removes the need for front and rear pointers.
DCircular queue efficiently uses all array slots by wrapping around, avoiding wasted space after dequeues.
Attempts:
2 left
💡 Hint

Think about what happens to space in a simple array queue after many dequeues.

Practice

(1/5)
1. What is the main characteristic of a queue implemented using an array?
easy
A. It follows First-In-First-Out (FIFO) order
B. It follows Last-In-First-Out (LIFO) order
C. It allows random access to elements
D. It automatically sorts elements

Solution

  1. Step 1: Understand queue behavior

    A queue adds elements at the rear and removes from the front, following FIFO.
  2. Step 2: Compare options

    LIFO is for stacks, random access and sorting are not queue properties.
  3. Final Answer:

    It follows First-In-First-Out (FIFO) order -> Option A
  4. Quick Check:

    Queue = FIFO [OK]
Hint: Queue always removes the oldest element first [OK]
Common Mistakes:
  • Confusing queue with stack (LIFO)
  • Thinking queue allows random access
  • Assuming queue sorts elements automatically
2. Which of the following is the correct way to initialize an empty queue using an array in Python?
easy
A. queue = ()
B. queue = {}
C. queue = set()
D. queue = []

Solution

  1. Step 1: Identify array type in Python

    Python lists are used as arrays and initialized with [] for empty list.
  2. Step 2: Check other options

    {} is dict, () is tuple, set() is a set, none are arrays.
  3. Final Answer:

    queue = [] -> Option D
  4. Quick Check:

    Empty array = [] [OK]
Hint: Use [] to create empty list (array) in Python [OK]
Common Mistakes:
  • Using {} which creates a dictionary
  • Using () which creates a tuple
  • Using set() which is unordered
3. What will be the output of the following code?
queue = []
queue.append(10)
queue.append(20)
queue.append(30)
print(queue.pop(0))
print(queue)
medium
A. 30 [10, 20]
B. 10 [20, 30]
C. 10 [10, 20, 30]
D. 20 [10, 30]

Solution

  1. Step 1: Trace queue operations

    Append adds 10, 20, 30 to queue: [10, 20, 30]. pop(0) removes first element 10.
  2. Step 2: Check print outputs

    First print outputs 10, second print outputs remaining queue [20, 30].
  3. Final Answer:

    10 [20, 30] -> Option B
  4. Quick Check:

    pop(0) removes front = 10 [OK]
Hint: pop(0) removes front element in queue [OK]
Common Mistakes:
  • Using pop() instead of pop(0) removes last element
  • Confusing append order
  • Expecting pop(0) to remove last element
4. Identify the error in this queue implementation using an array:
queue = []
queue.append(1)
queue.append(2)
queue.pop()
print(queue)
medium
A. pop() removes last element, not front
B. append() is used incorrectly
C. queue should be a dictionary
D. print statement syntax error

Solution

  1. Step 1: Understand pop() behavior

    pop() without index removes last element, not front as queue requires.
  2. Step 2: Check queue correctness

    Queue must remove front element, so pop(0) should be used instead of pop().
  3. Final Answer:

    pop() removes last element, not front -> Option A
  4. Quick Check:

    Queue removal = pop(0) front [OK]
Hint: Use pop(0) to dequeue from front [OK]
Common Mistakes:
  • Using pop() removes last, not front
  • Confusing append usage
  • Thinking queue is a dictionary
5. Given a fixed-size array of length 3, what happens if you try to enqueue 4 elements in a queue implemented using this array without resizing or circular logic?
hard
A. The queue automatically resizes to fit 4 elements
B. The 4th element replaces the first element
C. The queue overflows and cannot add the 4th element
D. The queue removes the last element to add the 4th

Solution

  1. Step 1: Understand fixed-size array limits

    An array of length 3 can hold only 3 elements; no extra space for 4th.
  2. Step 2: Check enqueue behavior without resizing

    Without resizing or circular logic, adding 4th element causes overflow error or failure.
  3. Final Answer:

    The queue overflows and cannot add the 4th element -> Option C
  4. Quick Check:

    Fixed array size = no extra elements [OK]
Hint: Fixed array size limits enqueue capacity [OK]
Common Mistakes:
  • Assuming automatic resizing happens
  • Thinking 4th element replaces first
  • Believing queue removes last element automatically
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard