Match a Descendant Selector in a DOM Tree
Updated
Problem statement
A simplified DOM contains nodes numbered from 0 to parent.length - 1. Node 0 is the root, parent[0] == -1, and parent[i] is the parent of node i. The tag name of node i is tags[i].
The space-separated selector is a descendant selector made only of tag names. A node matches when its own tag equals the last selector token and the earlier tokens can be matched, in order, by distinct ancestors; matched ancestors do not need to be adjacent.
Return the indices of all matching nodes in increasing order.
Function
matchSelector(parent: int[], tags: String[], selector: String) → int[]Examples
Example 1
parent = [-1,0,0,1,1,2]tags = ["html","body","aside","section","span","span"]selector = "body span"return = [4]Node 4 is a span below body; node 5 belongs to the aside branch.
Example 2
parent = [-1,0,1,2]tags = ["html","main","article","button"]selector = "html article button"return = [3]The main node may appear between matched descendant-selector tokens.
Constraints
1 <= parent.length == tags.length <= 100000.parent[0] == -1and0 <= parent[i] < ifor everyi > 0.- Every tag and selector token contains lowercase English letters and has length from
1to20. - The selector contains between
1and20tokens separated by single spaces.
Source note: The source image is a source-neutral excerpt of the author-reported prompt.
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
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- N-Ary Tree Codec OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Construct Binary Tree S-ExpressionOptiver · OAOptiver interview questions
- Count Parent Nodes With a Target SumAdobe · OAAdobe interview questions
- Recover a Tree from Preorder Depth EncodingSuperhuman · PHONE SCREENSuperhuman interview questions
- Diagonal Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
- Render an Org Chart and Find Skip-Level PairsSnap Inc. · ONSITE INTERVIEWSnap Inc. 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
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)