Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon OA
Problem: A binary tree is given by level order [1, 2, 3, 4, 5, None, 6]. Its diameter is the largest number of edges on any path between two nodes. What is the diameter?
- A. 4
- B. 5
- C. 3
- D. 6
Correct answer: A. 4
At each node, the best path through it joins the heights of its two subtrees. Taking the height of the tree alone gives the depth, not the longest path.
More Coding Interview Problems questions
- Problem: How many palindromic substrings of even length does "abbaccdd" contain?
- Problem: The string "aaaa" can be written as a concatenation of the pieces "a" and "aa" in several ways (order matters). How many such…
- Problem: The minimum ASCII delete sum to make the strings "sea" and "eat" equal is the least total ASCII value of deleted characters. Give…
- Problem: A pattern-matching task uses the string "abxcabcyabd" and the target "abc". How many length-3 windows of the text differ from…
- Problem: Using shell-style wildcards ( matches any run of characters including empty, ? matches exactly one character), how many words in…
- Problem: Choose a subsequence of [2, 3, 5, 7, 8] with no two chosen elements at adjacent positions. How many such non-empty choices sum to…