FastPrepCascade Deletion of Target-Valued Leaves
Problems/Cascade Deletion of Target-Valued Leaves

Cascade Deletion of Target-Valued Leaves

Microsoft logoMicrosoft● MediumINTERNPHONE SCREEN

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The ML internship technical screen explicitly requires cascade deletion of target-valued leaves. The report omits bounds and root/empty-tree cases; this exercise makes those cases explicit. Approximate source match: 95%.

Given a binary tree and an integer target, delete every leaf whose value equals target. Repeat this rule when deletion makes another matching node a leaf, until no matching leaf remains. Return the root of the remaining tree.

For this exercise, assume the empty tree remains empty and the root may also be deleted. Preserve the values and relative links of surviving nodes.

Function

removeTargetLeaves(root: TreeNode, target: int) → TreeNode

Examples

Example 1

root = [1,2,3,2,null,2,4]target = 2return = [1,null,3,null,4]
root
122324
Removed
→
return
134

Removing matching leaves can expose another removable matching leaf.

Example 2

root = [2,2,2]target = 2return = []
root
222
return
empty

The entire tree disappears when every node is removable.

Example 3

root = []target = 2return = []
root
empty
return
empty

An empty tree remains empty.

Constraints

  • For this exercise, assume a finite acyclic binary tree containing 0 through 500 nodes.
  • -10^6 <= node.val <= 10^6. The same bound applies to target.

More Microsoft Phone Screen questions

  • Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
  • Corporation Reporting LinesPHONE SCREEN · Seen Oct 2026
  • Random Forest Classifier from ScratchPHONE SCREEN · Seen Oct 2026
  • Least Recently Used CacheOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Greedy and Beam Sentence DecodingPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Top K Frequent ElementsPHONE SCREEN · Seen Sep 2026
  • 3SumPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Arithmetic Expression EvaluatorPHONE SCREEN · Seen Sep 2026
  • Employee Hierarchy Cycle and Depth AnalysisPHONE SCREEN · Seen Sep 2026
  • Most Visited String SectorsPHONE SCREEN · Seen Sep 2026

Similar questions at other companies

  • Lowest Common Ancestor in a BST and a Binary TreeGreyOrange · PHONE SCREENGreyOrange interview questions
  • N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • House Robber IIIOkta · PHONE SCREENOkta interview questions
  • Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
  • Validate Binary Search TreeKotak Mahindra Bank · PHONE SCREENKotak Mahindra Bank interview questions
  • House Robber IIIGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
  • Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
  • House Robber IIIVisa · PHONE SCREENVisa interview questions
  • Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
  • Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
All Microsoft interview questionsSee Microsoft 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 TreeNode removeTargetLeaves(TreeNode root, int target) {
        // Write your code here
    }
}
Checking account…