Palindrome Ancestor Path Queries
Asked at Uber · Coding Questions · Difficulty: Hard · Last asked Mar 22, 2026
A tree of `treeNodes` nodes (numbered `0..treeNodes-1`, rooted at node `0`) stores hierarchical data. You are given a string `s` of length `treeNodes` where `s[i]` is the lowercase character at node `i`, and edges via arrays `treeFrom`, `treeTo` (each length `treeNodes-1`). For each query node `q`, count how many nodes `v` on the path from `q` up to the root (including `q`) have the property that the characters on the path from `q` to `v` can be rearranged into a palindrome. A multiset of characters forms a palindrome iff at most one character has an odd frequency.
Loading the problem…