Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the top element of the stack.
DSA C
int peek(Stack *s) {
if (s->top == -1) {
return -1; // stack empty
}
return s->array[1];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong index like 0 or top-1.
Trying to access a variable named 'top' instead of s->top.
✗ Incorrect
The top element is at index s->top in the array.
2fill in blank
mediumComplete the code to check if the stack is empty before peeking.
DSA C
int peek(Stack *s) {
if ([1]) {
return -1; // stack empty
}
return s->array[s->top];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if top is 0 instead of -1.
Using greater than or less than comparisons incorrectly.
✗ Incorrect
The stack is empty when s->top is -1.
3fill in blank
hardFix the error in the peek function to correctly return the top element.
DSA C
int peek(Stack *s) {
if (s->top == -1) {
return -1;
}
return s->array[1];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
s->top + 1 which is out of bounds.Using
s->top - 1 which points to the wrong element.✗ Incorrect
The top element is at index s->top. Using s->top + 1 or others is incorrect.
4fill in blank
hardFill both blanks to complete the peek function that returns the top element safely.
DSA C
int peek(Stack *s) {
if ([1]) {
return -1;
}
return s->array[2];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking wrong condition for empty stack.
Accessing array at wrong index.
✗ Incorrect
Check if stack is empty with s->top == -1 and return the element at s->array[s->top].
5fill in blank
hardFill all three blanks to implement a safe peek function that returns the top element or -1 if empty.
DSA C
int peek(Stack *s) {
if ([1]) {
return [2];
}
return s->array[3];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning wrong value when empty.
Accessing array at wrong index.
✗ Incorrect
Check if stack is empty with s->top == -1, return -1 if empty, else return the top element at s->array[s->top].
