Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the maximum of two integers.
DSA Go
func max(a, b int) int { if a [1] b { return a } return b }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causes the function to return the smaller number.
Using '==' or '!=' does not compare sizes correctly.
✗ Incorrect
The function returns the larger of a and b, so the condition should be 'a > b'.
2fill in blank
mediumComplete the code to update the global maximum path sum if current sum is greater.
DSA Go
if currentSum [1] maxSum { maxSum = currentSum }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes maxSum to never update correctly.
Using '==' or '<=' does not cover all cases.
✗ Incorrect
We update maxSum only if currentSum is greater than maxSum.
3fill in blank
hardFix the error in the recursive function to return the maximum path sum including the current node.
DSA Go
func maxPathSumHelper(node *TreeNode) int {
if node == nil {
return 0
}
left := maxPathSumHelper(node.Left)
right := maxPathSumHelper(node.Right)
left = max(left, 0)
right = max(right, 0)
currentSum := node.Val + left + right
if currentSum > maxSum {
maxSum = currentSum
}
return node.Val + [1](left, right)
}Attempts:
3 left
💡 Hint
Common Mistakes
Using min returns the smaller sum, which is incorrect.
Using sum or abs are not valid functions here.
✗ Incorrect
We return the node's value plus the maximum of left or right path sums to continue the path upwards.
4fill in blank
hardFill both blanks to create a map of node values to their maximum path sums and filter nodes with sums greater than 10.
DSA Go
result := map[int]int{}
for _, node := range nodes {
sum := [1](node)
if sum [2] 10 {
result[node.Val] = sum
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using minPathSumHelper is incorrect as it does not exist or is irrelevant.
Using '<' filters sums less than 10, which is wrong.
✗ Incorrect
We compute max path sums using maxPathSumHelper and select sums greater than 10.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps node values to their max path sums if sums are positive.
DSA Go
result := map[int]int{}
for _, node := range nodes {
sum := [1](node)
if sum [2] 0 {
result[[3]] = sum
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using minPathSumHelper is incorrect.
Using sum <= 0 filters out positive sums incorrectly.
Using wrong key instead of node.Val.
✗ Incorrect
We use maxPathSumHelper to get sums, check if sum > 0, and map node.Val to sum.
