Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an opening bracket onto the stack.
DSA Python
if char in '({[': stack.[1](char)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() instead of append() to add elements.
Using remove() which deletes a specific element, not adding.
Using insert() without specifying index properly.
✗ Incorrect
We use append() to add an element to the top of the stack (list).
2fill in blank
mediumComplete the code to check if the stack is empty before popping.
DSA Python
if stack and stack[-1] == pairs[char]: stack.[1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using append() which adds instead of removing.
Using clear() which empties the whole list.
Using remove() which removes by value, not by position.
✗ Incorrect
pop() removes the last element from the stack, which is needed here.
3fill in blank
hardFix the error in the condition to check if the character is a closing bracket.
DSA Python
if char in [1]: if not stack or stack[-1] != pairs[char]: return False
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for opening brackets instead of closing brackets.
Using unrelated characters like letters or numbers.
Using incorrect bracket types.
✗ Incorrect
We check if char is a closing bracket, so it must be in '})]'.
4fill in blank
hardFill both blanks to create a dictionary mapping closing to opening brackets and check stack emptiness.
DSA Python
pairs = [1] return not [2](stack)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Incorrect bracket mappings in the dictionary.
Using len(stack) instead of bool(stack) for emptiness check.
Using any() or all() which do not check emptiness.
✗ Incorrect
The pairs dictionary maps closing to opening brackets correctly, and bool(stack) checks if stack is empty.
5fill in blank
hardFill all three blanks to complete the balanced parentheses function using stack.
DSA Python
def is_balanced(s): stack = [] pairs = [1] for char in s: if char in [2]: stack.[3](char) elif char in pairs: if not stack or stack[-1] != pairs[char]: return False stack.pop() return not stack
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing opening and closing brackets in pairs dictionary.
Using pop instead of append to add elements.
Checking wrong characters for opening brackets.
✗ Incorrect
pairs dictionary maps closing to opening brackets, opening brackets are '({[', and append adds to stack.