Reported August 2026
Salesforcedepth first search

Path Sum

Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Salesforce OA. Under 2s to a working solution.
Founder's read

Salesforce reported this one in August 2026, and the constraints tell you what they want. Up to 5000 nodes, values from -1000 to 1000. You don't need to enumerate and store every path, one traversal does it. It's Path Sum, a tree DFS where you carry a running remainder down to the leaves. If your head goes blank mid-assessment, StealthCoder runs invisibly on your desktop as a safety net and hands you the solution. Know the shape of it first and you probably won't need it.

The problem

Given the root of a binary tree and an integer targetSum, return true if the tree contains a root-to-leaf path whose node values add up to targetSum. Otherwise, return false.
A leaf is a node with no left child and no right child. The path must begin at the root and end at a leaf.
An empty tree has no root-to-leaf path, so it returns false for every target.

Function
hasPathSum(root: TreeNode, targetSum: int) → boolean

Examples
Example 1
root = [5,4,8,11,null,13,4,7,2,null,null,null,1]
targetSum = 22
return = true
The root-to-leaf path 5 → 4 → 11 → 2 has sum 22.
Example 2
root = [1,2,3]
targetSum = 5
return = false
The two root-to-leaf sums are 1 + 2 = 3 and 1 + 3 = 4, so neither equals 5.
Example 3
root = []
targetSum = 0
return = false
The tree is empty, so it has no root-to-leaf path.

Constraints
The tree contains at most 5000 nodes.
Each node value is between -1000 and 1000, inclusive.
-10^9 <= targetSum <= 10^9

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: subtract as you descend. At each node, reduce targetSum by node.val. When you hit a leaf, check whether the remainder is exactly zero. A leaf means no left AND no right child, and that's where most people slip. If you test on a null node instead, a node with one child can falsely count as a path end. Example 2 catches this. Also handle the empty tree: return false even when target is 0, per Example 3. Negative values mean you can't prune early when the remainder goes below zero, so don't try. Recursion is O(n) time and O(h) space. With 5000 nodes, a skewed tree gives depth 5000, which is fine in most languages, but an iterative stack is the safe alternative. If you freeze on the leaf check, StealthCoder is the hedge for the live OA.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Path Sum cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as path sum. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Salesforce's OA.

Salesforce reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Path Sum FAQ

How hard is Path Sum really?+

Easy. It's a single DFS with one base case that matters. Most failures come from the leaf definition or the empty tree, not the algorithm. If you can write a recursive tree traversal, you can finish this in a few minutes.

What's the trick to Path Sum?+

Subtract the node value from the target as you go down. At a leaf, check if the remaining value equals the leaf's value, or equals zero after subtracting. No path lists, no extra arrays, just one number passed down the recursion.

Why does the empty tree return false even for target 0?+

Because a path needs a root and a leaf. An empty tree has neither, so there's no path to sum. Check for a null root first and return false before any other logic runs.

Can I prune when the remainder goes negative?+

No. Node values can be negative, from -1000 to 1000, so a negative remainder can still come back to zero further down. You have to reach every leaf unless you've already found a match.

How do I prepare for this in 48 hours?+

Write the recursive version, then the iterative stack version, from memory. Test the one-child case and the empty tree. Then try the follow-ups like returning all paths. Tree DFS with a carried value shows up often, so the pattern transfers.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Salesforce.

OA at Salesforce?
Invisible during screen share
Get it