Palindromic paths from a node to the root
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Uber's online assessment, last asked May 2026. It tests trees, bitmask and dfs.
A storage system keeps its directories in a tree. Each directory holds one lowercase letter as a label. You are given:
-
arr: a list ofnlowercase letters;arr[i]is the label of nodei. Nodes are numbered0ton - 1, and node0is the root. -
tree_fromandtree_to: two lists of lengthn - 1. Edgekconnectstree_from[k]andtree_to[k]. Edges are undirected; either end may be the parent. -
queries: a list ofmnode ids.
For a query node q, look at every node v on the path from q up to the root, including q and the root. Call v good if the labels on the path from q to v (both ends included) can be rearranged into a palindrome.
Return a list where entry i is the number of good nodes for queries[i].
def palindromic_ancestor_counts(
arr: list[str], tree_from: list[int], tree_to: list[int], queries: list[int]
) -> list[int]: ...
Constraints
-
1 <= n <= 10^5,1 <= m <= 10^5 - The edges form a tree; queries may repeat.
- The tree can be a single long chain.
Example 1
arr = ['c', 'd', 'd', 'c']
tree_from = [0, 1, 1]
tree_to = [1, 2, 3]
queries = [2, 3]
output = [3, 2]
Node 2 ('d'): "d", "dd", "ddc" all rearrange into palindromes, so 3. Node 3 ('c'): "c" works, "cd" does not, "cdc" works, so 2.
Example 2
arr = ['a', 'a', 'b']
tree_from = [1, 2]
tree_to = [0, 1]
queries = [2, 0]
output = [2, 1]
The edges are listed child-first. From node 2: "b" and "baa" work; "ba" does not.
Clarifying questions
- Does the count include
v = qitself (a single letter is always a palindrome) and the root? - Are the labels only lowercase
atoz? - Since edges are undirected, is node
0always the root regardless of edge order? - Can queries repeat, and should repeated queries be cheap?
What a strong answer covers
- A multiset of letters forms a palindrome when at most one letter has an odd count, so only the parity of each letter matters: a 26-bit mask.
- Stores
mask[v], the XOR of labels from the root down tov. The path fromqup to ancestorvhas maskmask[q] ^ mask[parent(v)], with mask0for the parent of the root. - Walks the tree once with a count of the masks on the current root path; at each node, the answer is the count of
mask[q]plus the count of each of the 26 one-bit neighbours ofmask[q].O(27 n)for every node, thenO(1)per query. - Builds the rooted tree from undirected edges with an iterative DFS, so a chain of
10^5nodes does not overflow the stack. - Tests both examples, a chain of one letter, and repeated queries.
Follow-ups
- Count good pairs
(u, v)anywhere in the tree, not just ancestor paths. - Labels change over time.
- Return the longest such path from each query node.
Where it's asked
- Uber · Online assessment · Last asked May 202690 min · Two problems on a HackerRank-based platform, camera and screen share on · No AI
What it tests
trees · bitmask · dfs · prefix xor
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
