Count Palindromic Ancestor Paths
Updated
Problem statement
You are given a rooted tree with nodes numbered from 0 to n - 1, where n is the number of nodes. The root is node 0. For every node i, parent[i] is its parent, and parent[0] = -1. Each node has one letter: node i has the letter labels.charAt(i).
Take a node u from queries. Walk up from u toward the root and stop at some ancestor v. The node u itself also counts as a choice for v. Collect the letters on every node from v to u, including both ends. Call v valid if you can reorder those letters into a palindrome, a string that reads the same forwards and backwards. For each u, count the valid ancestors v.
Return the counts in the same order as queries. Letters can be reordered into a palindrome exactly when at most one letter appears an odd number of times.
Function
countPalindromicAncestorPaths(parent: int[], labels: String, queries: int[]) → int[]Examples
Example 1
parent = [-1,0,0,1,1,2]labels = "abacba"queries = [3,4,5]return = [1,3,3]For node 3, only the one-node path is valid. For node 4, the paths beginning at nodes 0, 1, and 4 are all palindromic after rearrangement. All three ancestors of node 5 are valid as well.
Example 2
parent = [-1,0,1,2]labels = "aaaa"queries = [0,3]return = [1,4]Every path contains only a, so every ancestor of each queried node is valid.
Example 3
parent = [-1,0,1]labels = "abc"queries = [2]return = [1]The one-node path containing c is valid. The longer paths have two or three odd character counts.
Constraints
1 <= parent.length == labels.length <= 200000.parent[0] = -1, and fori > 0,0 <= parent[i] < i.labelscontains only lowercase English letters.1 <= queries.length <= 200000.- Every query is a valid node index; query nodes may repeat.
More Uber Online Assessment questions
- Jump Game with Prime-3 StepsOA · Seen Aug 2026
- Last Truck to Leave the LaneOA · Seen Jul 2026
- Chain of CommandOA · Seen Jul 2026
- Total Palindrome Substring CostOA · Seen Jun 2026
- Jump Game with Prime-Step RuleOA · Seen May 2026
- Pipeline Throughput Maximization Under BudgetOA · Seen May 2026
- Balanced Prefix Sets in a PermutationOA · Seen Apr 2026
- Minimum Edge Reversals to Root a TreeOA · Seen Apr 2026
- Maximum Riders in CarOA · Seen Apr 2026
- Graph Palindrome QueriesOA · Seen Apr 2026
Similar questions at other companies
- Palindromic Paths to the RootRippling · OARippling interview questions
- Maximum Sum of a k-Node Downward PathHudson River Trading · OAHudson River Trading interview questions
- Minimum Leaf Path Sum over All RootsInfosys · OAInfosys interview questions
- Part 2: Batch Access ReviewScale AI · OAScale AI interview questions
- Sum Dynamic Weighted Tree DistancesJuspay · OA
- Binary Tree CamerasAmazon · OA · PHONE SCREEN
- K-th Ancestor Subtree Preorder QueryAdobe · OAAdobe interview questions
- Maximum Sum Path Between Two Leaf NodesGoogle · OAGoogle interview questions
- Maximum Strength of Every NeuronMicrosoft · OAMicrosoft interview questions
- Delivery ServiceInMobi · OAInMobi interview questions