FastPrepPath Sum
Problems/Path Sum

Path Sum

Salesforce logoSalesforce● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Salesforce onsite interview. The report names Path Sum on a tree. It does not say where the path starts and ends or what to return. Our version is about 90% the same as the reported question.

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 = 22return = true
root
54117281341

The root-to-leaf path 5 → 4 → 11 → 2 has sum 22.

Example 2

root = [1,2,3]targetSum = 5return = false
root
123

The two root-to-leaf sums are 1 + 2 = 3 and 1 + 3 = 4, so neither equals 5.

Example 3

root = []targetSum = 0return = false
root
empty

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

More Salesforce Onsite Interview questions

  • Count Increasing TripletsONSITE INTERVIEW · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Reverse a Sublist of a Linked ListONSITE INTERVIEW · Seen Nov 2026
  • Rotting OrangesONSITE INTERVIEW · Seen Nov 2026
  • 3Sum SmallerONSITE INTERVIEW · Seen Nov 2026
  • Count Zero Request ServersONSITE INTERVIEW · Seen Nov 2026
  • Group Identical Tree LevelsONSITE INTERVIEW · Seen Oct 2026
  • Meeting Rooms IIONSITE INTERVIEW · Seen Oct 2026
  • Construct a Tree from Level-Order and Inorder TraversalsONSITE INTERVIEW · Seen Sep 2026
  • Maximize Monsters DefeatedONSITE INTERVIEW · Seen Sep 2026

Similar questions at other companies

  • Diameter of Binary TreeeBay · ONSITE INTERVIEWeBay interview questions
  • Count One-Valued Regions in a Binary TreeGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Maximum Depth of Binary TreeLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
  • Binary Tree Preorder TraversalAnduril · ONSITE INTERVIEWAnduril interview questions
  • Root-to-Node Path in a Binary TreeAmazon · ONSITE INTERVIEW
  • Symmetric Binary TreeFigma · ONSITE INTERVIEWFigma interview questions
  • Tree Level with the Most NodesBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
  • Diameter of a Binary TreeMeta · ONSITE INTERVIEWMeta interview questions
  • Check That Every Node Equals Its Descendant SumMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Mirror Image Binary TreesExpedia · ONSITE INTERVIEWExpedia interview questions

Salesforce interview experiences

  • Salesforce Infrastructure SWE OA and Onsite: 1 Experience
  • Salesforce Experienced Backend SWE First Round: 1 Experience
All Salesforce interview questionsSee Salesforce hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        // Write your solution here
    }
}
Checking account…