Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an element onto an array-based stack.
DSA Javascript
function push(stack, element) {
stack.[1](element);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using append which is not a JavaScript array method.
Using insert which is not a standard array method in JavaScript.
✗ Incorrect
In JavaScript, the push() method adds an element to the end of an array, which is how we add to the top of an array-based stack.
2fill in blank
mediumComplete the code to pop an element from a linked list stack.
DSA Javascript
function pop(stack) {
if (!stack.head) return null;
const value = stack.head.value;
stack.head = stack.head.[1];
return value;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is used in doubly linked lists but not singly linked lists.
Using 'link' or 'child' which are not standard property names.
✗ Incorrect
In a linked list, each node points to the next node using the 'next' property. To pop, we move the head to the next node.
3fill in blank
hardFix the error in the code that checks if an array stack is empty.
DSA Javascript
function isEmpty(stack) {
return stack.[1] === 0;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' which is used in some other languages or data structures.
Using 'count' or 'total' which are not JavaScript array properties.
✗ Incorrect
JavaScript arrays use the 'length' property to indicate how many elements they contain.
4fill in blank
hardFill both blanks to create a linked list node and link it as the new head of the stack.
DSA Javascript
function push(stack, value) {
const newNode = { value: value, [1]: stack.[2] };
stack.head = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is for backward links in doubly linked lists.
Using 'tail' which points to the end, not the top of the stack.
✗ Incorrect
The new node's 'next' points to the current head, then the head is updated to the new node.
5fill in blank
hardFill all three blanks to create a function that returns the top element of an array stack without removing it.
DSA Javascript
function peek(stack) {
if (stack.[1] === 0) return null;
return stack[stack.[2] [3] 1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' which is not a JavaScript array property.
Using '+' instead of '-' which would cause an index out of range.
✗ Incorrect
We check if the array length is zero, then return the last element at index length - 1.
