Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an element onto the stack.
DSA C
void push(Stack* s, int value) {
if (s->top < MAX_SIZE - 1) {
s->top[1];
s->items[s->top] = value;
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using decrement operator which moves the top down.
Using assignment operators incorrectly.
✗ Incorrect
We use the increment operator ++ to move the top index up by one before adding the new value.
2fill in blank
mediumComplete the code to dequeue an element from the queue.
DSA C
int dequeue(Queue* q) {
if (q->front != q->rear) {
int value = q->items[q->front];
q->front[1];
return value;
}
return -1; // Queue empty
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using decrement operator which moves front backward.
Not updating the front index.
✗ Incorrect
We use ++ to move the front index forward after removing the element.
3fill in blank
hardFix the error in the stack pop function to correctly return the top element.
DSA C
int pop(Stack* s) {
if (s->top >= 0) {
int value = s->items[s->top];
s->top[1];
return value;
}
return -1; // Stack empty
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Increasing the top index which causes wrong element removal.
Not updating the top index.
✗ Incorrect
We use -- to decrease the top index after popping the element.
4fill in blank
hardFill in the blank to check if the queue is empty and reset indices.
DSA C
int isEmpty(Queue* q) {
if (q->front [1] q->rear) {
q->front = 0;
q->rear = 0;
return 1;
}
return 0;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using != which checks for non-empty.
Using > or < which are incorrect for empty check.
✗ Incorrect
The queue is empty when front and rear are equal, so we check with ==.
5fill in blank
hardFill all three blanks to create a stack and push an element.
DSA C
Stack* createStack() {
Stack* s = (Stack*)malloc(sizeof([1]));
s->top = [2];
return s;
}
void push(Stack* s, int value) {
if (s->top < MAX_SIZE - 1) {
s->top[3];
s->items[s->top] = value;
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Allocating memory for wrong type.
Initializing top incorrectly.
Using wrong operator to update top.
✗ Incorrect
We allocate memory for Stack, initialize top to -1, and use ++ to increment top when pushing.
