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 a variable name that is not defined like 'val' or 'data'.
✗ Incorrect
The new node should be created with the variable 'value' which holds the data to insert.
2fill in blank
mediumComplete the code to check if the position is 1, meaning insert at the head.
DSA Typescript
if (position === [1]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for position 0 which is invalid in this context.
✗ Incorrect
Position 1 means the new node should be inserted at the head of the list.
3fill in blank
hardFix the error in the loop condition to move to the correct node before insertion.
DSA Typescript
while (current !== null && count < [1]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'position' directly causes overshooting the node.
✗ Incorrect
We stop at the node before the insertion position, so count must be less than position - 1.
4fill in blank
hardFill both blanks to correctly link the new node between previous and current nodes.
DSA Typescript
newNode.[1] = current; newNode.[2] = current.prev;
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping next and prev causing broken links.
✗ Incorrect
The new node's next points to current, and prev points to current's previous node.
5fill in blank
hardFill all three blanks to update the surrounding nodes to point to the new node.
DSA Typescript
if (current.prev !== null) { current.prev.[1] = newNode; } current.[2] = newNode; if (position === 1) { head = [3]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Not updating head when inserting at position 1.
Mixing up next and prev pointers.
✗ Incorrect
The previous node's next points to newNode, current's prev points to newNode, and head updates to newNode if inserting at position 1.
