Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an opening parenthesis onto the stack.
DSA C++
if (ch == '(') { stack.[1](ch); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() instead of push() to add elements.
Trying to access top() to add elements.
Using empty() which checks if stack is empty.
✗ 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 C++
if (!stack.[1]()) { stack.pop(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using top() which returns the top element, not a boolean.
Using size() which returns a number, not a boolean.
Using push() which adds elements instead of checking.
✗ Incorrect
empty() returns true if the stack has no elements, so !empty() means stack is not empty.
3fill in blank
hardFix the error in the condition to match closing parentheses correctly.
DSA C++
if (ch == ')' && stack.[1]() == '(') { stack.pop(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using empty() which returns a boolean, not the element.
Using size() which returns the number of elements.
Using push() which adds elements instead of checking.
✗ Incorrect
top() returns the element at the top of the stack to compare with '('.
4fill in blank
hardFill both blanks to check for matching pairs of brackets.
DSA C++
if (ch == '[1]' && stack.top() == '[2]') { stack.pop(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping opening and closing brackets.
Using brackets of different types incorrectly.
Checking wrong pairs like '(' with ']'
✗ Incorrect
')' matches with '(' and ']' matches with '['. Here we check for parentheses pair.
5fill in blank
hardComplete the code to complete the code snippet that counts bracket types.
DSA C++
std::map<char, int> counts; for (char ch : input) { if (ch == '(' || ch == ')') { counts[ch][1]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using decrement -- instead of increment ++.
Counting wrong bracket types.
Using wrong characters in conditions.
✗ Incorrect
We count '(' and ')' characters by incrementing their counts in the map.
