Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given value.
DSA Typescript
const newNode = new Node([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head' instead of 'value' when creating the new node.
Using 'next' or 'prev' which are pointers, not the data value.
✗ Incorrect
We create a new node by passing the value to the Node constructor.
2fill in blank
mediumComplete the code to link the new node's next pointer to the current head.
DSA Typescript
newNode.next = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode.next to itself causing a cycle.
Setting newNode.next to null which breaks the list.
✗ Incorrect
The new node's next pointer should point to the current head node.
3fill in blank
hardFix the error in updating the previous pointer of the old head node.
DSA Typescript
if (head !== null) { head.[1] = newNode; }
Attempts:
3 left
💡 Hint
Common Mistakes
Updating 'next' instead of 'prev' pointer.
Trying to update 'value' or 'head' which are not pointers.
✗ Incorrect
The old head's previous pointer should point back to the new node.
4fill in blank
hardFill both blanks to update the head pointer and the new node's previous pointer.
DSA Typescript
head = [1]; newNode.[2] = null;
Attempts:
3 left
💡 Hint
Common Mistakes
Setting head to old head instead of new node.
Setting newNode.next to null instead of newNode.prev.
✗ Incorrect
The head pointer should now point to the new node, and the new node's previous pointer should be null because it is the first node.
5fill in blank
hardFill all three blanks to complete the insertAtBeginning function.
DSA Typescript
function insertAtBeginning(head: Node | null, value: number): Node {
const newNode = new Node([1]);
newNode.next = [2];
if (head !== null) {
head.prev = newNode;
}
newNode.prev = null;
[3] = newNode;
return [3];
}Attempts:
3 left
💡 Hint
Common Mistakes
Not updating the head pointer to the new node.
Returning the old head instead of the new head.
Mixing up the order of assignments.
✗ Incorrect
We create the new node with the value, link its next to the current head, and then update the head pointer to the new node before returning it.
