Challenge - 5 Problems
Path Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of path sum check for a simple tree
What is the output of the following TypeScript code that checks if a path sum exists from root to leaf?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { this.val = val === undefined ? 0 : val; this.left = left === undefined ? null : left; this.right = right === undefined ? null : right; } } function hasPathSum(root: TreeNode | null, targetSum: number): boolean { if (!root) return false; if (!root.left && !root.right) return root.val === targetSum; return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val); } const tree = new TreeNode(5, new TreeNode(4, new TreeNode(11, new TreeNode(7), new TreeNode(2)), null), new TreeNode(8, new TreeNode(13), new TreeNode(4, null, new TreeNode(1)))); console.log(hasPathSum(tree, 22));
Attempts:
2 left
💡 Hint
Think about whether there is a root-to-leaf path that sums to 22.
✗ Incorrect
The tree has a path 5 -> 4 -> 11 -> 2 which sums to 22, so the function returns true.
❓ Predict Output
intermediate2:00remaining
Output when no path matches the target sum
What will be printed when the following code runs?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { this.val = val === undefined ? 0 : val; this.left = left === undefined ? null : left; this.right = right === undefined ? null : right; } } function hasPathSum(root: TreeNode | null, targetSum: number): boolean { if (!root) return false; if (!root.left && !root.right) return root.val === targetSum; return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val); } const tree = new TreeNode(1, new TreeNode(2), new TreeNode(3)); console.log(hasPathSum(tree, 5));
Attempts:
2 left
💡 Hint
Check if any root-to-leaf path sums to 5.
✗ Incorrect
Paths are 1->2 (sum 3) and 1->3 (sum 4), neither equals 5, so output is false.
🔧 Debug
advanced2:00remaining
Identify the error in path sum function
What error will this code produce when run?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { this.val = val === undefined ? 0 : val; this.left = left === undefined ? null : left; this.right = right === undefined ? null : right; } } function hasPathSum(root: TreeNode | null, targetSum: number): boolean { if (!root) return false; if (!root.left && !root.right) return root.val === targetSum; return hasPathSum(root.left, targetSum - root.val) && hasPathSum(root.right, targetSum - root.val); } const tree = new TreeNode(1, new TreeNode(2), new TreeNode(3)); console.log(hasPathSum(tree, 3));
Attempts:
2 left
💡 Hint
Check the logical operator used in the recursive call.
✗ Incorrect
The code uses '&&' which requires both left and right paths to have the sum, but only one path needs to match. So it returns false.
❓ Predict Output
advanced2:00remaining
Output of path sum with negative values
What is the output of this code that includes negative values in the tree?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { this.val = val === undefined ? 0 : val; this.left = left === undefined ? null : left; this.right = right === undefined ? null : right; } } function hasPathSum(root: TreeNode | null, targetSum: number): boolean { if (!root) return false; if (!root.left && !root.right) return root.val === targetSum; return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val); } const tree = new TreeNode(-2, null, new TreeNode(-3)); console.log(hasPathSum(tree, -5));
Attempts:
2 left
💡 Hint
Consider the path -2 -> -3 and its sum.
✗ Incorrect
The path -2 -> -3 sums to -5, so the function returns true.
🧠 Conceptual
expert3:00remaining
Number of root-to-leaf paths with given sum
Given a binary tree, how many root-to-leaf paths sum to the target value 8 in the tree below?
Tree structure:
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
Options:
Attempts:
2 left
💡 Hint
Find all root-to-leaf paths and sum their values.
✗ Incorrect
No root-to-leaf paths sum to 8. The path sums are: 5+4+11+7=27, 5+4+11+2=22, 5+8+13=26, 5+8+4+1=18. None equal 8.