Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an opening bracket onto the stack.
DSA Javascript
if (char === '(') { stack.[1](char); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push removes elements instead of adding.
Using shift or unshift changes the wrong end of the array.
✗ Incorrect
We use push() to add an element to the top of the stack.
2fill in blank
mediumComplete the code to check if the stack is empty before popping.
DSA Javascript
if (stack.[1] === 0) { return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using size or count which are not valid properties for arrays in JavaScript.
Using empty which is not a property.
✗ Incorrect
The length property gives the number of elements in an array.
3fill in blank
hardFix the error in popping the top element from the stack.
DSA Javascript
const top = stack.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using push which adds elements instead of removing.
Using shift or unshift which operate on the start of the array.
✗ Incorrect
pop() removes and returns the last element from the array, which is the top of the stack.
4fill in blank
hardFill both blanks to check if the popped bracket matches the current closing bracket.
DSA Javascript
if (top [1] '(' && char [2] ')') { continue; } else { return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using != or !== which check for inequality.
Using == which is less strict and can cause bugs.
✗ Incorrect
We use strict equality (===) to compare values exactly.
5fill in blank
hardFill all three blanks to complete the function that checks balanced parentheses.
DSA Javascript
function isBalanced(str) {
const stack = [];
for (let i = 0; i < str.[1]; i++) {
const char = str[i];
if (char === '(') {
stack.[2](char);
} else if (char === ')') {
if (stack.[3] === 0) {
return false;
}
stack.pop();
}
}
return stack.length === 0;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push to add elements.
Checking stack.pop() === 0 which is incorrect.
✗ Incorrect
We loop using str.length, push opening brackets, and check stack.length before popping.
