DSA interview question
An inventory system models warehouses as a binary tree. Every warehouse's stock count must equal the sum of its two sub-warehouses' counts, with a missing child counting as 0, and leaves hold whatever they hold. An auditor must verify that every internal node satisfies this "children sum property". A traversal checks each node against its children. The whole tree passes only if every internal node does.
Example: [10, 8, 2, 3, 5, 1, 1] → true (10 = 8 + 2, 8 = 3 + 5, 2 = 1 + 1).
bool isSumProperty(TreeNode* root);
| Input (level order) | Output | Explanation |
|---|---|---|
[10, 8, 2, 3, 5, 1, 1] | true | |
[35, 20, 15, 15, 5, 10, 5] | true | |
[1, 4, 3, 5] | false | 1 ≠ 4 + 3 |
[3, 1, 2, 2] | false | 1 has a single child 2 |
[5] | true | A leaf has no constraint |
[] | true |
(Confirm: the rule applies to every node with at least one child; a missing child counts as 0; leaves are always fine.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in