Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the queue size in the stack structure.
DSA C
typedef struct {
int arr[100];
int front, rear;
} Queue;
typedef struct {
Queue q;
int [1];
} Stack;
void initStack(Stack* s) {
s->[1] = 0;
s->q.front = 0;
s->q.rear = -1;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' or 'length' which are not updated properly.
Confusing queue indices with stack top.
✗ Incorrect
The 'top' variable keeps track of the number of elements in the stack.
2fill in blank
mediumComplete the code to enqueue an element into the queue.
DSA C
void enqueue(Queue* q, int val) {
q->rear++;
q->arr[q->[1]] = val;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'front' instead of 'rear' causes overwriting the wrong position.
Using 'top' which is unrelated to queue indices.
✗ Incorrect
We insert the new value at the 'rear' index of the queue.
3fill in blank
hardFix the error in the pop function to correctly remove the top element from the stack using the queue.
DSA C
int pop(Stack* s) {
if (s->top == 0) return -1; // empty
int i, val;
for (i = 0; i < s->top - 1; i++) {
val = s->q.arr[s->q.front];
s->q.front++;
s->q.arr[++s->q.rear] = val;
}
val = s->q.arr[s->q.front];
s->q.front++;
s->top--;
[1];
return val;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not updating rear causes incorrect queue state.
Setting rear equal to front causes empty queue misinterpretation.
✗ Incorrect
After removing elements, rear should be reset to front - 1 to maintain queue state.
4fill in blank
hardFill both blanks to implement the push function that adds an element to the stack using the queue.
DSA C
void push(Stack* s, int val) {
s->q.arr[++s->q.[1]] = val;
s->top++;
int i, size = s->top;
for (i = 0; i < size - 1; i++) {
int temp = s->q.arr[s->q.[2]];
s->q.front++;
s->q.arr[++s->q.rear] = temp;
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using front instead of rear to insert new element.
Not rotating elements correctly after insertion.
✗ Incorrect
We add the new element at rear and then rotate elements from front to rear to simulate stack push.
5fill in blank
hardFill all three blanks to implement the isEmpty function that checks if the stack is empty.
DSA C
int isEmpty(Stack* s) {
if (s->[1] == 0 && s->q.[2] > s->q.[3]) {
return 1;
}
return 0;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking only top or only queue indices.
Using size which is not maintained.
✗ Incorrect
Stack is empty if top is zero and queue front is greater than rear (empty queue).
