Selected Tree Nodes at the Same Level and Height
Updated
Problem statement
You are given a rooted tree with n nodes, where n is the length of the array parent. The nodes are numbered from 0 through n - 1, and parent describes the tree. parent[0] = -1, which means node zero has no parent and is the root. For every other node i, parent[i] is its parent.
You are also given selected, a list of node numbers with no repeats. Return two booleans. The first says whether all selected nodes have the same depth from the root. The second says whether they all have the same height. A leaf has height 0. Any other node's height is one more than the largest height among its children.
Function
sameLevelAndHeight(parent: int[], selected: int[]) → boolean[]Examples
Example 1
parent = [-1,0,0,1,1,2]selected = [3,4,5]return = [true,true]All three nodes are leaves at depth 2, so they share both depth and height.
Example 2
parent = [-1,0,0,1,3,2]selected = [1,2]return = [true,false]Nodes 1 and 2 share depth 1, but their heights are 2 and 1.
Constraints
1 <= parent.length <= 100000parent[0] = -1- For
1 <= i < parent.length,0 <= parent[i] < i 1 <= selected.length <= parent.length- Selected node numbers are distinct and valid
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Diagonal Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
- Render an Org Chart and Find Skip-Level PairsSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Find Leaves of a Binary TreeLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions
- JSON Path Lookup with Implicit Array WildcardsMongoDB · ONSITE INTERVIEWMongoDB interview questions
- Delete and Compact a Forest SubtreePinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Onion-Peel Layers of a Binary TreeeBay · ONSITE INTERVIEWeBay interview questions