Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add an element to the stack.
DSA Typescript
push(element: number): void {
this.items.[1](element);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'append' which is not a valid array method in TypeScript.
✗ Incorrect
The push method adds an element to the end of the array representing the stack.
2fill in blank
mediumComplete the code to remove the top element from the stack.
DSA Typescript
pop(): number | undefined {
return this.items.[1]();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'shift' which removes the first element, not the last.
✗ Incorrect
The pop method removes and returns the last element of the array, which is the top of the stack.
3fill in blank
hardFix the error in the method that returns the top element without removing it.
DSA Typescript
peek(): number | undefined {
return this.items[this.items.[1] - 1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'length()' which is not a valid property but a function call.
✗ Incorrect
The length property gives the number of elements in the array. We subtract 1 to get the last index.
4fill in blank
hardFill both blanks to check if the stack is empty.
DSA Typescript
isEmpty(): boolean {
return this.items.[1] === [2];
}Attempts:
3 left
💡 Hint
Common Mistakes
Comparing length to null or -1 which are incorrect.
✗ Incorrect
The stack is empty if the array length is zero.
5fill in blank
hardFill all three blanks to clear the stack.
DSA Typescript
clear(): void {
this.items = [1];
this.size = [2];
this.top = [3];
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting size or top to null instead of numbers.
✗ Incorrect
To clear the stack, reset the items to an empty array, size to zero, and top index to -1.
