Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the stack array.
DSA Typescript
private stack: number[] = new Array[1];Attempts:
3 left
💡 Hint
Common Mistakes
Initializing with a fixed size like 10 causes wasted space.
Using parentheses incorrectly or missing them.
✗ Incorrect
The stack array is initialized with size 0 to start empty and resize dynamically.
2fill in blank
mediumComplete the code to check if the stack is empty.
DSA Typescript
isEmpty(): boolean {
return this.top [1] 0;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' or '>' which give wrong empty condition.
Using '<=' which includes negative values that don't apply here.
✗ Incorrect
The stack is empty when top equals 0, meaning no elements are present.
3fill in blank
hardFix the error in the push method to resize the array when full.
DSA Typescript
push(value: number): void {
if (this.top === this.stack.length) {
const newStack = new Array(this.stack.length * 2 [1] 1);
for (let i = 0; i < this.stack.length; i++) {
newStack[i] = this.stack[i];
}
this.stack = newStack;
}
this.stack[this.top] = value;
this.top++;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' or '/' which changes the size incorrectly.
Using '-' which can cause negative size.
✗ Incorrect
We add 1 to handle the case when length is zero to avoid zero size array.
4fill in blank
hardFill both blanks to complete the pop method safely.
DSA Typescript
pop(): number | undefined {
if (this.isEmpty()) {
return [1];
}
this.top--;
const value = this.stack[[2]];
return value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning null instead of undefined.
Accessing stack at top - 1 after decrementing top.
✗ Incorrect
Return undefined if empty, and access the element at current top index before decrement.
5fill in blank
hardFill all three blanks to implement the peek method correctly.
DSA Typescript
peek(): number | undefined {
if (this.isEmpty()) {
return [1];
}
return this.stack[[2] [3] 1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causing wrong index.
Returning null instead of undefined.
✗ Incorrect
Return undefined if empty, then return element at top - 1 index without removing it.
