Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the current node is nil.
DSA Go
if [1] == nil { return true }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'root' instead of 'node' causes confusion in recursion.
Checking for 'tree' or 'rootNode' which are not defined in this scope.
✗ Incorrect
We check if the current node (named 'node') is nil to stop recursion.
2fill in blank
mediumComplete the code to check if the current node's value is within the allowed range.
DSA Go
if node.Val <= [1] || node.Val >= [2] { return false }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' or 'right' instead of min and max confuses the range check.
Using >= for min or <= for max reverses the logic.
✗ Incorrect
The node's value must be greater than min and less than max to be valid in BST.
3fill in blank
hardFix the error in the recursive call to validate the left subtree with updated max boundary.
DSA Go
return isValidBSTHelper(node.Left, [1], node.Val) && isValidBSTHelper(node.Right, node.Val, [2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping min and max in recursive calls.
Passing nil instead of boundaries causes runtime errors.
✗ Incorrect
For the left subtree, max becomes node.Val; for the right subtree, min becomes node.Val.
4fill in blank
hardFill both blanks to initialize the min and max values for the first call to isValidBST.
DSA Go
func isValidBST(root *TreeNode) bool {
return isValidBSTHelper(root, [1], [2])
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 for boundaries restricts valid values incorrectly.
Using nil causes type errors in Go.
✗ Incorrect
We start with the smallest and largest possible integer values as boundaries.
5fill in blank
hardFill all three blanks to complete the helper function signature and recursive calls correctly.
DSA Go
func isValidBSTHelper(node *TreeNode, [1] int64, [2] int64) bool { if node == nil { return true } if node.Val <= [1] || node.Val >= [2] { return false } return isValidBSTHelper(node.Left, [1], node.Val) && isValidBSTHelper(node.Right, node.Val, [2]) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent parameter names causes confusion.
Passing wrong variables in recursive calls.
✗ Incorrect
The helper takes min and max as int64 boundaries; node.Val is checked against min and max.