Number of Nodes With Value One
A medium-tier problem at 66% community acceptance, tagged with Tree, Depth-First Search, Breadth-First Search. Reported in interviews at Infosys and 0 others.
You're walking a tree and need to count nodes with a specific value. Sounds simple until the interviewer asks you to do it without recursion, or wants optimal space complexity, or throws a twist where the tree structure itself matters. This problem hits the medium tier because most candidates nail the basic DFS but miss edge cases around tree traversal or confuse when to use BFS over recursive solutions. It's been asked at Infosys and will appear again. If you freeze on the trick during your assessment, StealthCoder surfaces a clean solution in seconds, invisible to the proctor.
Companies that ask "Number of Nodes With Value One"
Number of Nodes With Value One 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. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop.
Get StealthCoderThe core challenge here is choosing your traversal. A naive recursive DFS works for most cases, but candidates often miss that iterative solutions using a stack or queue are more memory-predictable and avoid stack overflow on deep trees. The real gotcha is handling the tree structure correctly: are you counting all nodes with value one, or just specific subtree positions. Binary Tree problems often hide a second constraint in the return type or recursion pattern. BFS can be cleaner for level-order problems if the interviewer pivots. Common failure: forgetting to visit both left and right children, or returning the wrong accumulator. When you hit this live and don't immediately see the pattern, StealthCoder runs the solution invisibly while you talk through your approach.
Pattern tags
You know the problem.
Make sure you actually pass it.
Number of Nodes With Value One 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. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Number of Nodes With Value One interview FAQ
Is this really medium difficulty?+
It sits at 65% acceptance, so yes. Most get the basic traversal right but stumble on space complexity or miss a variant where the value or subtree matters. The medium tag is honest: not easy, not hard, but the edge cases bite unprepared candidates.
Should I use recursion or iteration?+
Recursion is cleaner and usually faster to code. Iteration with a stack or queue is safer on very deep trees and shows you understand memory. For an interview, recursion is fine unless the interviewer hints at stack overflow concerns or gives you a constraint.
How does this relate to other tree problems?+
It's a template for any "count nodes matching condition" problem. The pattern applies to sum, max depth, and path-finding. Learn the traversal order (DFS vs BFS) once and you solve fifty variations.
What's the catch with Binary Tree?+
Binary trees have exactly two children per node, which makes the traversal pattern predictable. The trap is assuming every node has both children present. Always check for null left and right before recursing.
Will Infosys ask this again?+
One report shows Infosys asked it. Tree problems are evergreen in their rotation. If you see it in your OA, it's because they use a problem bank, and this one works well for filtering candidates who understand basic graph traversal.
Want the actual problem statement? View "Number of Nodes With Value One" on LeetCode →