CODEBHAVYA • DATA STRUCTURES

🚶 Queue

Learn the FIFO principle, understand enqueue/dequeue/front/rear operations, visualize a linear array queue, and trace the exact C program step by step.

📖 Queue Overview

A queue is a linear data structure in which insertion happens at one end, called the rear, and deletion happens at the other end, called the front.

💡 FIFO Principle

Queue follows First In, First Out (FIFO). The element inserted first is the first element removed.

10
FRONT
20
waiting
30
REAR

🧠 Real-Life Example

Think about people standing in a ticket queue. The person who joins first is normally served first. New people join at the rear, while service happens from the front.

⚙️ Queue Operations

Enqueue Insert a new element at the REAR.
Dequeue Remove the element at the FRONT.
Front / Peek Read the first element without removing it.
Rear Read the most recently inserted element.
Important: In a simple linear array queue, front and rear move only forward. Therefore, after rear == MAX - 1, no new element can be inserted even if earlier array positions became free after dequeues. Circular Queue solves this limitation.

🧱 Queue Using Array

A linear array queue uses two integer variables: front identifies the first valid element and rear identifies the last valid element.

#define MAX 5

int queue[MAX];

int front = -1;
int rear  = -1;

Empty Queue

Initially, front = -1 and rear = -1. When the first element is enqueued, front becomes 0.

➕ Enqueue Operation

ENQUEUE(value)

1. If rear == MAX - 1
      Queue Overflow

2. If front == -1
      front = 0

3. rear = rear + 1

4. queue[rear] = value

Example

If the queue contains 10, 20, 30 with front = 0 and rear = 2, then Enqueue(40) makes rear = 3 and stores 40 at queue[3].

➖ Dequeue Operation

DEQUEUE()

1. If front == -1
      Queue Underflow

2. value = queue[front]

3. If front == rear
      front = rear = -1
   Else
      front = front + 1

4. Return value

Example

For 10, 20, 30, Dequeue removes 10. After the operation, front moves from index 0 to index 1.

👀 Front and Rear Operations

Front returns queue[front] and Rear returns queue[rear]. Neither operation removes an element.

FRONT()
    if front == -1
        Queue is empty
    else
        return queue[front]

REAR()
    if rear == -1
        Queue is empty
    else
        return queue[rear]
INTERACTIVE ALGORITHM VISUALIZATION
CodeBhavya CodeBhavya
🎬 Premium Linear Array Queue Visualizer Watch FRONT and REAR move during multiple queue operations
Configure the queue operation and press Load / Apply.

💻 Linear Array Queue Program in C

Visible Learning Program

#include <stdio.h>

#define MAX 5

int queue[MAX];
int front = -1;
int rear = -1;

void enqueue(int value)
{
    if(rear == MAX - 1)
    {
        printf("Queue Overflow\n");
        return;
    }

    if(front == -1)
        front = 0;

    rear++;
    queue[rear] = value;
}

int dequeue()
{
    int value;

    if(front == -1)
        return -1;

    value = queue[front];

    if(front == rear)
    {
        front = -1;
        rear = -1;
    }
    else
    {
        front++;
    }

    return value;
}

int peekFront()
{
    if(front == -1)
        return -1;

    return queue[front];
}

int peekRear()
{
    if(rear == -1)
        return -1;

    return queue[rear];
}

int main()
{
    enqueue(10);
    enqueue(20);
    enqueue(30);

    printf("Dequeued: %d\n", dequeue());

    enqueue(40);

    printf("Front: %d\n", peekFront());
    printf("Rear: %d\n", peekRear());

    return 0;
}

Program Output

Dequeued: 10
Front: 20
Rear: 40

Final Queue

20 30 40

front = 1
rear  = 3

⚡ Linear Queue Complexity

Enqueue — O(1) Insertion happens directly at REAR.
Dequeue — O(1) Removal moves FRONT by one position.
Front / Rear — O(1) Both values are accessed directly.
Space — O(MAX) The fixed array reserves MAX positions.
Interview note: A simple linear queue can waste free positions at the beginning of the array after dequeues. Do not shift all elements after every dequeue because that would make Dequeue O(n). Use a Circular Queue to reuse freed positions while keeping operations O(1).

🔄 Circular Queue

A circular queue treats the last array position as connected back to the first. This lets REAR reuse positions that became free after dequeue operations.

💡 Why Circular Queue?

A linear array queue can waste positions before FRONT. Circular Queue reuses those positions without shifting elements, so the fixed array is used more efficiently.

Move REAR rear = (rear + 1) % MAX
Move FRONT front = (front + 1) % MAX
Queue Full (rear + 1) % MAX == front

🧠 Wrap-Around Example

For capacity 5, if rear = 4 and index 0 is free, the next REAR becomes (4 + 1) % 5 = 0. That is the key circular movement.

FeatureLinear QueueCircular Queue
Reuse freed positionsNoYes
REAR movementOnly forwardWraps using modulo
False overflowPossibleAvoided
Enqueue / DequeueO(1)O(1)

⚙️ Circular Queue Operations

Enqueue Algorithm

CIRCULAR_ENQUEUE(value)

1. If (rear + 1) % MAX == front
      Queue Overflow

2. If front == -1
      front = 0

3. rear = (rear + 1) % MAX

4. queue[rear] = value

Dequeue Algorithm

CIRCULAR_DEQUEUE()

1. If front == -1
      Queue Underflow

2. value = queue[front]

3. If front == rear
      front = rear = -1
   Else
      front = (front + 1) % MAX

4. Return value
Important: For MAX = 5, modulo creates this index movement: 0 → 1 → 2 → 3 → 4 → 0 → 1 ....
INTERACTIVE ALGORITHM VISUALIZATION
CodeBhavya CodeBhavya
🎬 Premium Circular Queue Visualizer Watch FRONT and REAR move and see REAR wrap from the last index back to index 0
Circular Queue front = -1 • rear = -1
FRONT-1
REAR-1
Current Size0
Capacity5
Wrap-Around Demo shows why Circular Queue can reuse earlier positions.

💻 Circular Queue Program in C

Visible Learning Program

#include <stdio.h>

#define MAX 5

int queue[MAX];
int front = -1;
int rear = -1;

int isFull()
{
    return (rear + 1) % MAX == front;
}

void enqueue(int value)
{
    if(isFull())
    {
        printf("Queue Overflow\n");
        return;
    }

    if(front == -1)
        front = 0;

    rear = (rear + 1) % MAX;
    queue[rear] = value;
}

int dequeue()
{
    int value;

    if(front == -1)
        return -1;

    value = queue[front];

    if(front == rear)
    {
        front = -1;
        rear = -1;
    }
    else
    {
        front = (front + 1) % MAX;
    }

    return value;
}

int peekFront()
{
    if(front == -1)
        return -1;

    return queue[front];
}

int peekRear()
{
    if(rear == -1)
        return -1;

    return queue[rear];
}

int main()
{
    enqueue(10);
    enqueue(20);
    enqueue(30);
    enqueue(40);

    printf("Dequeued: %d\n", dequeue());
    printf("Dequeued: %d\n", dequeue());

    enqueue(50);
    enqueue(60);

    printf("Front: %d\n", peekFront());
    printf("Rear: %d\n", peekRear());

    return 0;
}

Program Output

Dequeued: 10
Dequeued: 20
Front: 30
Rear: 60

Final Circular Queue

Logical order:
30 40 50 60

front = 2
rear  = 0

REAR wrapped to index 0.

⚡ Circular Queue Complexity

Enqueue — O(1)REAR moves with one modulo calculation.
Dequeue — O(1)FRONT moves with one modulo calculation.
Front / Rear — O(1)Both values are directly accessible.
Space — O(MAX)Freed positions can be reused.
Interview note: In this implementation, the queue is full when (rear + 1) % MAX == front, while the empty condition is front == -1.

🔗 Queue Using Linked List

A queue can also be implemented with a linked list. Instead of reserving a fixed array, each queue element is stored in a dynamically allocated node.

💡 FRONT and REAR Pointers

FRONT points to the first node, which will be removed next. REAR points to the last node, where the next node will be inserted.

10
next
FRONT
20
next
middle
30
NULL
REAR
struct Node
{
    int data;
    struct Node *next;
};

struct Node *front = NULL;
struct Node *rear  = NULL;
Important: With a linked-list queue, there is no fixed array capacity. Overflow occurs only when dynamic memory allocation fails.

⚙️ Enqueue and Dequeue Using Linked List

Enqueue

ENQUEUE(value)

1. Create newNode
2. newNode->data = value
3. newNode->next = NULL

4. If rear == NULL
      front = rear = newNode
      return

5. rear->next = newNode
6. rear = newNode

Dequeue

DEQUEUE()

1. If front == NULL
      Queue Underflow

2. temp = front
3. value = temp->data

4. front = front->next

5. If front == NULL
      rear = NULL

6. free(temp)
7. return value

🧠 Key Edge Case

When the final node is dequeued, front becomes NULL. At that moment, rear must also become NULL; otherwise REAR would point to freed memory.

INTERACTIVE ALGORITHM VISUALIZATION
CodeBhavya CodeBhavya
🎬 Premium Linked List Queue Visualizer Watch nodes enter at REAR and leave from FRONT
Load the queue and choose an operation.

💻 Queue Using Linked List — C Program

Visible Learning Program

#include <stdio.h>
#include <stdlib.h>

struct Node
{
    int data;
    struct Node *next;
};

struct Node *front = NULL;
struct Node *rear = NULL;

void enqueue(int value)
{
    struct Node *newNode =
        (struct Node *)malloc(sizeof(struct Node));

    newNode->data = value;
    newNode->next = NULL;

    if(rear == NULL)
    {
        front = newNode;
        rear = newNode;
        return;
    }

    rear->next = newNode;
    rear = newNode;
}

int dequeue()
{
    struct Node *temp;
    int value;

    if(front == NULL)
        return -1;

    temp = front;
    value = temp->data;

    front = front->next;

    if(front == NULL)
        rear = NULL;

    free(temp);

    return value;
}

int peekFront()
{
    if(front == NULL)
        return -1;

    return front->data;
}

int peekRear()
{
    if(rear == NULL)
        return -1;

    return rear->data;
}

int main()
{
    enqueue(10);
    enqueue(20);
    enqueue(30);

    printf("Dequeued: %d\n", dequeue());

    enqueue(40);

    printf("Front: %d\n", peekFront());
    printf("Rear: %d\n", peekRear());

    return 0;
}

Program Output

Dequeued: 10
Front: 20
Rear: 40

Final Queue

FRONT → 20 → 30 → 40 → NULL
                       ↑
                      REAR

⚡ Linked List Queue Complexity

Enqueue — O(1)Insert directly using REAR.
Dequeue — O(1)Remove directly using FRONT.
Front / Rear — O(1)Both pointers give direct access.
Space — O(n)One dynamically allocated node per element.
Interview note: A linked-list queue needs both FRONT and REAR pointers to guarantee O(1) insertion and deletion. If only FRONT were maintained, reaching the last node for every enqueue would require O(n) traversal.

↔️ Deque (Double Ended Queue)

A Deque is a queue in which insertion and deletion are allowed at both ends. The name Deque comes from Double Ended Queue.

💡 Main Idea

A normal queue inserts at REAR and deletes from FRONT. A Deque is more flexible: it can insert and delete at both FRONT and REAR.

Insert FrontAdd a value before the current FRONT.
Insert RearAdd a value after the current REAR.
Delete FrontRemove the current FRONT value.
Delete RearRemove the current REAR value.
Input-Restricted Deque Insertion is allowed at only one end, while deletion is allowed at both ends.
Output-Restricted Deque Deletion is allowed at only one end, while insertion is allowed at both ends.
Important: An array-based Deque is usually implemented circularly so that both FRONT and REAR can wrap around and reuse freed positions in O(1) time.

⚙️ Deque Algorithms

Insert at Front

INSERT_FRONT(value)

1. If Deque is full
      Overflow

2. If Deque is empty
      front = rear = 0

3. Else if front == 0
      front = MAX - 1

4. Else
      front = front - 1

5. deque[front] = value

Insert at Rear

INSERT_REAR(value)

1. If Deque is full
      Overflow

2. If Deque is empty
      front = rear = 0

3. Else if rear == MAX - 1
      rear = 0

4. Else
      rear = rear + 1

5. deque[rear] = value

Delete at Front

DELETE_FRONT()

1. If Deque is empty
      Underflow

2. value = deque[front]

3. If front == rear
      front = rear = -1

4. Else if front == MAX - 1
      front = 0

5. Else
      front = front + 1

6. Return value

Delete at Rear

DELETE_REAR()

1. If Deque is empty
      Underflow

2. value = deque[rear]

3. If front == rear
      front = rear = -1

4. Else if rear == 0
      rear = MAX - 1

5. Else
      rear = rear - 1

6. Return value
INTERACTIVE ALGORITHM VISUALIZATION
CodeBhavya CodeBhavya
🎬 Premium Deque Visualizer Insert and delete from both FRONT and REAR with circular wrap-around
FRONT-1
REAR-1
Size0
Capacity6
FRONT and REAR can move in either direction and wrap around the array.
Two-End Demo shows insertion and deletion from both ends.

💻 Circular Array Deque Program in C

Visible Learning Program

#include <stdio.h>

#define MAX 6

int deque[MAX];
int front = -1;
int rear = -1;

int isEmpty()
{
    return front == -1;
}

int isFull()
{
    return (front == 0 && rear == MAX - 1) ||
           (front == rear + 1);
}

void insertFront(int value)
{
    if(isFull())
    {
        printf("Deque Overflow\n");
        return;
    }

    if(isEmpty())
    {
        front = 0;
        rear = 0;
    }
    else if(front == 0)
    {
        front = MAX - 1;
    }
    else
    {
        front--;
    }

    deque[front] = value;
}

void insertRear(int value)
{
    if(isFull())
    {
        printf("Deque Overflow\n");
        return;
    }

    if(isEmpty())
    {
        front = 0;
        rear = 0;
    }
    else if(rear == MAX - 1)
    {
        rear = 0;
    }
    else
    {
        rear++;
    }

    deque[rear] = value;
}

int deleteFront()
{
    int value;

    if(isEmpty())
        return -1;

    value = deque[front];

    if(front == rear)
    {
        front = -1;
        rear = -1;
    }
    else if(front == MAX - 1)
    {
        front = 0;
    }
    else
    {
        front++;
    }

    return value;
}

int deleteRear()
{
    int value;

    if(isEmpty())
        return -1;

    value = deque[rear];

    if(front == rear)
    {
        front = -1;
        rear = -1;
    }
    else if(rear == 0)
    {
        rear = MAX - 1;
    }
    else
    {
        rear--;
    }

    return value;
}

int getFront()
{
    if(isEmpty())
        return -1;

    return deque[front];
}

int getRear()
{
    if(isEmpty())
        return -1;

    return deque[rear];
}

int main()
{
    insertRear(20);
    insertRear(30);
    insertFront(10);
    insertRear(40);

    printf("Deleted Front: %d\n", deleteFront());
    printf("Deleted Rear: %d\n", deleteRear());

    insertFront(5);
    insertRear(50);

    printf("Front: %d\n", getFront());
    printf("Rear: %d\n", getRear());

    return 0;
}

Program Output

Deleted Front: 10
Deleted Rear: 40
Front: 5
Rear: 50

Final Logical Deque

5 20 30 50

front = 5
rear  = 2

FRONT wrapped to index 5.

⚡ Deque Complexity

Insert Front — O(1)FRONT moves directly.
Insert Rear — O(1)REAR moves directly.
Delete Front — O(1)FRONT moves directly.
Delete Rear — O(1)REAR moves directly.
Interview note: Deque is useful when a problem needs efficient insertion and deletion at both ends. It is commonly used in sliding-window problems, palindrome checks, task scheduling, and monotonic queue techniques.

⭐ Priority Queue

A Priority Queue stores elements together with a priority. Removal is based on priority rather than only on insertion order.

💡 Priority Rule Used Here

In this implementation, a larger priority number means higher priority. If two elements have the same priority, the one inserted earlier is removed first.

Max Priority Queue The element with the largest priority value is served first.
Min Priority Queue The element with the smallest priority value is served first.

🧠 Example

For (10,2), (20,5), (30,3), (40,5), the highest priority is 5. Since 20 was inserted before 40, 20 is removed first.

Important: Priority Queue is an abstract data structure. It can be implemented using arrays, linked lists, binary heaps, or other structures. This page first teaches a simple unsorted-array implementation.

⚙️ Priority Queue Operations

Enqueue

ENQUEUE(data, priority)

1. If count == MAX
      Overflow

2. queue[count].data = data
3. queue[count].priority = priority
4. count = count + 1

Find Highest Priority

HIGHEST_PRIORITY_INDEX()

1. If count == 0
      return -1

2. index = 0

3. For i = 1 to count - 1
      If queue[i].priority > queue[index].priority
            index = i

4. return index

Delete Highest Priority

DEQUEUE_HIGHEST()

1. Find highest-priority index
2. Save its data
3. Shift later items one position left
4. count = count - 1
5. Return removed data
Visualizer input format: Use data:priority, for example 10:2,20:5,30:3.
INTERACTIVE ALGORITHM VISUALIZATION
CodeBhavya CodeBhavya
🎬 Premium Priority Queue Visualizer Observe priority scanning, stable ties, insertion, and highest-priority removal
Count0
Highest Index
Highest Data
Highest Priority
Equal priorities preserve insertion order because only a strictly greater priority replaces the current highest.
Priority Demo scans for the highest priority and removes it.

💻 Priority Queue Using Array — C Program

Visible Learning Program

#include <stdio.h>

#define MAX 6

struct Item
{
    int data;
    int priority;
};

struct Item queue[MAX];
int count = 0;

void enqueue(int data, int priority)
{
    if(count == MAX)
    {
        printf("Priority Queue Overflow\n");
        return;
    }

    queue[count].data = data;
    queue[count].priority = priority;
    count++;
}

int highestPriorityIndex()
{
    int index = 0;

    if(count == 0)
        return -1;

    for(int i = 1; i < count; i++)
    {
        if(queue[i].priority > queue[index].priority)
            index = i;
    }

    return index;
}

int dequeueHighest()
{
    int index;
    int value;

    if(count == 0)
        return -1;

    index = highestPriorityIndex();
    value = queue[index].data;

    for(int i = index; i < count - 1; i++)
        queue[i] = queue[i + 1];

    count--;

    return value;
}

int peekHighest()
{
    int index;

    if(count == 0)
        return -1;

    index = highestPriorityIndex();

    return queue[index].data;
}

int main()
{
    enqueue(10, 2);
    enqueue(20, 5);
    enqueue(30, 3);
    enqueue(40, 5);

    printf("Removed: %d\n", dequeueHighest());
    printf("Highest: %d\n", peekHighest());

    enqueue(50, 6);

    printf("Highest: %d\n", peekHighest());

    return 0;
}

Program Output

Removed: 20
Highest: 40
Highest: 50

Final Priority Queue

(10,2) (30,3) (40,5) (50,6)

count = 4
highest item = 50
highest priority = 6

⚡ Priority Queue Complexity

Enqueue — O(1)Append to the unsorted array.
Find Highest — O(n)Scan all current items.
Dequeue Highest — O(n)Scan plus left shifting.
Space — O(MAX)Fixed array of priority items.
Interview note: For production-quality Priority Queues, a binary heap is usually preferred: peek is O(1), while insertion and deletion are O(log n). The simple array version is useful for understanding the Priority Queue behavior first.

📊 Queue Types — Complete Comparison

The best Queue implementation depends on how elements must be inserted, removed, prioritized, and stored. Use this table as a quick revision before interviews and placements.

Queue Type Main Rule Enqueue / Insert Delete Space Important Advantage Main Limitation / Note
Linear Array Queue FIFO O(1) O(1) O(MAX) Simple implementation May waste positions before FRONT and cause false overflow
Circular Queue FIFO + wrap-around O(1) O(1) O(MAX) Reuses freed array positions Needs careful full/empty conditions
Linked Queue FIFO O(1) O(1) O(n) Dynamic size; no fixed array capacity Extra pointer memory and dynamic allocation
Deque Both-end access O(1) at both ends O(1) at both ends O(MAX) or O(n) Very flexible two-end operations More pointer/index cases than a normal Queue
Priority Queue Priority-based service Depends on implementation Depends on implementation O(n) Serves the most important item first Heap implementation is preferred for efficient updates
Placement shortcut: Use a normal Queue for FIFO, Circular Queue for fixed reusable buffers, Linked Queue for dynamic FIFO storage, Deque for both-end operations, and Priority Queue when service order depends on importance rather than arrival time.

🌍 Real-World Applications of Queue

🖥️ CPU Scheduling Ready processes are commonly maintained in queues; Round Robin uses a circular style of repeated service.
🖨️ Printer Spooling Print jobs wait in arrival order and are processed one by one.
🌐 Network Buffers Packets wait in buffers before transmission or processing.
🕸️ Breadth-First Search BFS uses a FIFO Queue to visit vertices level by level.
🚑 Emergency Scheduling Priority Queues can serve urgent tasks before lower-priority tasks.
🪟 Sliding Window Problems Deque is useful for maintaining candidates efficiently in window maximum/minimum problems.
⌨️ Input / I/O Buffers Keyboard, streaming, and device data often wait in Queue-like buffers.
🎫 Customer Service Ticket counters and service systems naturally model FIFO waiting lines.
📨 Message Processing Applications and distributed systems use message queues to process work asynchronously.

❓ Common Queue Interview Questions

1. What is FIFO?
FIFO means First In, First Out. The element inserted earliest is removed first.
Interview answer: Queue removes elements in the same order in which they were inserted.
2. What are queue overflow and underflow?
For this fixed linear array queue, overflow occurs when rear == MAX - 1. Underflow occurs when a dequeue or peek is attempted while front == -1.
Interview answer: Overflow means no usable array position remains at the rear; underflow means the queue is empty.
3. What is the main drawback of a linear array queue?
After several dequeues, positions before FRONT become unused. Since REAR keeps moving forward, those free positions cannot be reused by the basic linear queue.
Interview answer: A linear queue can suffer false overflow because freed positions at the beginning are wasted; Circular Queue fixes this.
4. How do you detect a full circular queue?
The queue is full when the position immediately after REAR would become FRONT: (rear + 1) % MAX == front.
Interview answer: If the next circular REAR position equals FRONT, no free insertion position remains.
5. Why is Circular Queue better than Linear Queue for a fixed array?
Circular Queue reuses array positions freed by dequeue operations instead of permanently losing them.
Interview answer: It prevents wasted front-side positions and avoids false overflow.
6. Why is modulo used in Circular Queue?
Modulo keeps FRONT and REAR inside the valid array range and creates wrap-around. For example, (4 + 1) % 5 = 0.
Interview answer: Modulo converts movement beyond the final index back to index 0.
7. Why do we maintain both FRONT and REAR in a linked-list queue?
FRONT gives O(1) access to the node that must be removed next, while REAR gives O(1) access to the insertion point.
Interview answer: Maintaining both pointers keeps both enqueue and dequeue O(1).
8. What must happen when the last node is dequeued?
After removing the final node, FRONT becomes NULL. REAR must also be set to NULL so it does not point to freed memory.
Interview answer: For an empty linked queue, both FRONT and REAR must be NULL.
9. What is the main advantage of linked-list queue over array queue?
The linked-list implementation grows dynamically and is not restricted by a fixed array capacity. Its practical limit is available dynamic memory.
Interview answer: Linked Queue avoids fixed-capacity limitations while preserving O(1) enqueue and dequeue.
10. What is a Deque?
A Deque is a Double Ended Queue that supports insertion and deletion at both FRONT and REAR.
Interview answer: Deque generalizes a queue by allowing O(1) operations at both ends.
11. What is the difference between input-restricted and output-restricted Deque?
In an input-restricted Deque, insertion is restricted to one end but deletion is allowed at both ends. In an output-restricted Deque, deletion is restricted to one end but insertion is allowed at both ends.
Interview answer: One restricts insertion; the other restricts deletion.
12. Why is a circular array useful for Deque implementation?
Circular indexing lets FRONT and REAR wrap around and reuse free positions without shifting elements.
Interview answer: Circular storage keeps both-end insertions and deletions O(1) while reusing the fixed array efficiently.
13. How is Priority Queue different from a normal Queue?
A normal queue serves elements mainly by FIFO order. A Priority Queue serves the element with the highest or lowest priority according to the chosen rule.
Interview answer: Normal Queue is order-driven; Priority Queue is priority-driven.
14. Why is a binary heap commonly used for Priority Queue?
A binary heap keeps the highest- or lowest-priority item at the root. It provides O(1) peek and O(log n) insertion/deletion.
Interview answer: Heap gives a strong balance between fast access and efficient updates.
15. How are equal priorities handled in this array implementation?
The scan updates the highest index only when it finds a strictly greater priority. Therefore, among equal priorities, the earlier inserted item remains selected first.
Interview answer: This implementation behaves stably for equal priorities.
16. Which Queue is used in Breadth-First Search?
BFS uses a normal FIFO Queue. A discovered vertex is inserted at REAR and processed from FRONT.
Interview answer: BFS uses FIFO Queue so vertices are processed level by level.
17. Can a Queue be implemented using two Stacks?
Yes. One common method pushes new elements into one stack and transfers elements to a second stack only when the second stack is empty and a dequeue/peek is needed.
Interview answer: Two stacks can implement FIFO Queue with amortized O(1) operations.
18. What is false overflow in a linear Queue?
False overflow happens when REAR reaches the final array position even though earlier positions became free after dequeue operations.
Interview answer: The array still has unused space, but a simple linear Queue cannot reuse it; Circular Queue solves this.
19. What is the difference between Deque and Priority Queue?
Deque controls operations by end position—FRONT or REAR. Priority Queue controls removal by priority, regardless of the physical end.
Interview answer: Deque is position-based; Priority Queue is priority-based.
20. When should you prefer Circular Queue over Linked Queue?
Prefer Circular Queue when capacity is known and fixed memory reuse is desirable, such as bounded buffers. Prefer Linked Queue when the number of elements changes dynamically and fixed capacity is undesirable.
Interview answer: Circular Queue is ideal for bounded reusable storage; Linked Queue is ideal for dynamic growth.

🎯 20 Queue Practice Problems

Try every problem yourself first. Use 💻 Solve It Yourself to write and test your C program. Open Hint only when necessary, and use Show Program when you want to study the complete solution.

📈 Queue Practice Progress
Solved 0 / 20
Completed with Solution 0
Total Score 0 / 2000
Completion 0%

🏆 Scoring

Pass all 5 tests without help for up to 100 points. Opening a hint caps the competitive score at 90. Opening the official program still lets you complete the problem, but it is recorded as Completed rather than competitively solved.

← Previous Topic: Stack Next Topic: Trees →