Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA
Problem: A binary tree is encoded in preorder with '#' for a missing child: "1,2,#,#,3,4,#,#,5,#,#". How many leaves (nodes with two missing children) does it have?
- A. 4
- B. 3
- C. 2
- D. 5
Correct answer: B. 3
Rebuild the tree from the preorder string, with each '#' consuming a missing child. A leaf is a value node whose two children are both absent; counting '#' tokens counts missing children, not leaves.
More Coding Interview Problems questions
- Problem: Count the palindromic substrings of length exactly 4 in the string "aabbaabbaa".
- Problem: How many distinct two-letter substrings does "mississippi" contain?
- Problem: The smallest period of a string s is the least p ≥ 1 such that s[i] = s[i+p] for all valid i. What is the smallest period of…
- Problem: What is the length of the longest proper border (a proper prefix that is also a suffix) of "abacabacaba"? A proper border is…
- Problem: How many runs (maximal blocks of equal consecutive characters) does "aabbbaaaacc" contain?
- Problem: Houses are arranged in a circle with values [2, 3, 2, 5, 1]. You may rob any set of houses with no two adjacent in the circle…