Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the front element of the queue.
DSA C
int peekFront(struct Queue* q) {
if (q->front == -1) {
return -1; // Queue is empty
}
return q->array[q->[1]];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using q->array[q->rear] instead of q->array[q->front]
Returning q->size or q->capacity instead of an element
Not checking if the queue is empty before accessing
✗ Incorrect
The front element of the queue is at index 'front' in the array.
2fill in blank
mediumComplete the code to check if the queue is empty before peeking.
DSA C
int peekFront(struct Queue* q) {
if (q->[1] == -1) {
return -1; // Queue is empty
}
return q->array[q->front];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking rear instead of front for emptiness
Checking size or capacity which may not reflect emptiness
Not returning -1 when queue is empty
✗ Incorrect
The queue is empty if the front index is -1.
3fill in blank
hardFix the error in the peek function to correctly handle empty queue.
DSA C
int peekFront(struct Queue* q) {
if ([1]) {
return -1; // Queue is empty
}
return q->array[q->front];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking front == 0 instead of front == -1
Using rear or size incorrectly to check emptiness
✗ Incorrect
The queue is empty when front is -1, not 0.
4fill in blank
hardFill both blanks to correctly implement peekFront with empty check and return front element.
DSA C
int peekFront(struct Queue* q) {
if (q->[1] == -1) {
return -1; // Queue is empty
}
return q->array[q->[2]];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using rear instead of front for empty check
Returning element at rear instead of front
✗ Incorrect
Check if front is -1 to detect empty queue, then return element at front index.
5fill in blank
hardFill all three blanks to implement peekFront with empty check, return front element, and print error if empty.
DSA C
int peekFront(struct Queue* q) {
if (q->[1] == -1) {
printf("[2]\n");
return [3];
}
return q->array[q->front];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not printing error message when queue is empty
Returning wrong error code
Checking wrong index for emptiness
✗ Incorrect
Check front == -1 for empty, print message, and return -1 as error code.
