MEDIUMasked at 1 company

Number of Nodes in the Sub-Tree With the Same Label

A medium-tier problem at 55% community acceptance, tagged with Hash Table, Tree, Depth-First Search. Reported in interviews at Samsung and 0 others.

Founder's read

Samsung asks this one regularly, and it trips up candidates who treat tree traversal as separate from counting. You're given a tree and labels on each node. The task: for every node, count how many nodes in its subtree share that node's label. It looks straightforward until you realize you need to traverse the entire subtree for each node without recomputing work. Acceptance is just above 55%, meaning half the people who attempt it get stuck on the traversal pattern or the counting logic. If this hits your live assessment and you freeze on the approach, StealthCoder runs invisibly and surfaces a working solution in seconds.

Companies asking
1
Difficulty
MEDIUM
Acceptance
55%

Companies that ask "Number of Nodes in the Sub-Tree With the Same Label"

If this hits your live OA

Number of Nodes in the Sub-Tree With the Same Label is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know.

Get StealthCoder
What this means

The trick is DFS with a running count map. You traverse down, track label frequencies in the subtree, and bubble counts back up to the parent. The naive approach does a full recount at each node, which tanks performance. The pattern: each recursive call returns a dictionary of label counts from its subtree, then merges results from child calls and updates the answer array. The pitfall is thinking you need to track absolute node indices vs. using the label map directly. Common failure point: not understanding that you're counting occurrence of the same label within a single subtree, not across the entire tree. Hash Table makes this efficient; if you skip it and use a list-based approach, you'll time out. This is exactly the kind of problem where the obvious DFS works, but only after you see the optimization. StealthCoder hedges the live OA moment when the first approach doesn't pass all tests.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Number of Nodes in the Sub-Tree With the Same Label recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Number of Nodes in the Sub-Tree With the Same Label interview FAQ

Is this really a Samsung problem or just one report?+

Samsung is listed as the company reporting it. Frequency data is limited, but if you're interviewing there, treat it as a known risk. The pattern (DFS with aggregation) shows up across multiple problem families, so learning this boosts your odds on similar tree questions.

What's the actual trick I'm missing if my DFS times out?+

You're likely recounting labels at every node. Use a Hash Table (dict/map) to aggregate label counts from child subtrees in one pass. Return the count map from each recursive call instead of recomputing the entire subtree. This cuts complexity from O(n^2) to O(n).

Is this harder than typical DFS problems?+

The DFS mechanics are standard. The difficulty spike is the counting aggregation and remembering that you're counting per-label within a subtree, not globally. Medium difficulty (55% acceptance) is fair; it's not a pure tree walk, it requires merging results from children.

Do I need to know BFS for this, or is DFS enough?+

DFS is the core pattern here. BFS is listed as a topic but BFS doesn't naturally fit the subtree aggregation needed for this problem. DFS lets you build results bottom-up and return them to parents cleanly.

How does this relate to other tree + counting problems?+

It's a hybrid of tree traversal and frequency counting. If you've solved problems on path sums or node distance, the traversal style is familiar. The new piece is maintaining a count map and merging results from multiple children in a single pass.

Want the actual problem statement? View "Number of Nodes in the Sub-Tree With the Same Label" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.