Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given value.
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = [1]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to undefined or 0 instead of null.
✗ Incorrect
When creating a new node, its next pointer should start as null because it doesn't point to any other node yet.
2fill in blank
mediumComplete the push method to add a new node at the top of the stack.
DSA Typescript
push(value: number): void {
const newNode = new Node(value);
newNode.next = this.top;
this.top = [1];
this.size++;
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning top to value or size instead of the new node.
✗ Incorrect
To add a new node on top, we set the stack's top to the new node.
3fill in blank
hardFix the error in the pop method to correctly remove and return the top node's value.
DSA Typescript
pop(): number | null {
if (!this.top) return null;
const poppedNode = this.top;
this.top = poppedNode.next;
this.size--;
return [1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning the node object or the new top's value instead of the popped node's value.
✗ Incorrect
We return the value of the popped node, not the node itself or other properties.
4fill in blank
hardFill both blanks to check if the stack is empty and return a boolean.
DSA Typescript
isEmpty(): boolean {
return this.size [1] 0;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using > or !== which give wrong boolean results for emptiness.
✗ Incorrect
The stack is empty if its size is exactly zero.
5fill in blank
hardFill all three blanks to implement peek method that returns the top value without removing it.
DSA Typescript
peek(): number | null {
if (this.top === [1]) return null;
return this.top[2][3];
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning undefined or accessing value without checking if top is null.
✗ Incorrect
If top is null, return null. Otherwise, return top.value using dot notation.
