Reported April 2026
Uberbit manipulation

Palindrome Path Queries in a Tree

Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Uber OA. Under 2s to a working solution.
Founder's read

The mistake that sinks a first attempt on this Uber OA, reported April 2026, is walking the path for every query. It looks fine on the examples and dies on large trees. This is a tree problem with a bitmask twist: for each query [u, v], you need to know if the letters on the path can be rearranged into a palindrome. That means at most one letter has an odd count. If you blank on the trick, StealthCoder runs invisibly during the live assessment as a safety net. But the idea is short, so learn it first.

The problem

You are given an undirected tree with n nodes numbered from 0 to n - 1. The tree is rooted at node 0, and each node i has a lowercase English character labels[i].
You are also given a list of queries. For each query [u, v], consider all node characters encountered on the simple path from u to v, including both endpoints.
Return an integer array where the answer for each query is 1 if the characters on that path can be rearranged to form a palindrome, and 0 otherwise.

Function
palindromePathQueries(n: int, edges: int[][], labels: String, queries: int[][]) → int[]

Examples
Example 1
n = 5
edges = [[0,1],[0,2],[1,3],[1,4]]
labels = "ababa"
queries = [[3,4],[2,3],[2,4]]
return = [1,1,0]
For [3,4], the path characters are b,b,a, which can form bab. For [2,3], the characters are a,a,b,b, which can form a palindrome. For [2,4], the characters are a,a,b,a, with two odd character counts, so they cannot form a palindrome.
Example 2
n = 4
edges = [[0,1],[1,2],[1,3]]
labels = "abca"
queries = [[0,2],[2,3],[1,1]]
return = [0,0,1]
The first two paths contain three different characters, so neither can be rearranged into a palindrome. A path from a node to itself always contains one character.

Constraints
n == labels.length
edges.length == n - 1
edges forms a valid tree rooted at node 0.
labels contains only lowercase English letters.
queries[i].length == 2

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is parity. Give each letter one bit in a 26-bit mask. For every node, compute the XOR of labels from the root down to that node. The path from u to v has mask pref[u] ^ pref[v] ^ bit(label[lca]), because the LCA is cancelled out by the two prefixes and needs to be added back once. Then check popcount(mask) <= 1, or mask & (mask - 1) == 0. The common pitfall is forgetting the LCA bit, which breaks the answer when the LCA's letter matters, like the [2,4] case in example 1. Another pitfall is recomputing paths per query. Do a DFS or BFS from node 0 to build prefix masks and depths, then use binary lifting for LCA. Preprocessing is O(n log n) and each query is O(log n). If the LCA code goes sideways under pressure, StealthCoder is the hedge in the live OA.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Palindrome Path Queries in a Tree cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Uber's OA.

Uber reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Palindrome Path Queries in a Tree FAQ

What's the trick in Palindrome Path Queries in a Tree?+

Use a 26-bit parity mask per node. A multiset of characters can form a palindrome if at most one letter has an odd count. XOR of the root-to-node masks gives the path parity, once you add back the LCA's own bit.

Why do I need the LCA?+

pref[u] ^ pref[v] cancels everything from the root to the LCA, including the LCA's own letter. That node is on the path, so you XOR its bit back in. Skipping this fails on queries where the LCA's letter flips parity.

How hard is this one really?+

Medium to medium-hard. The bitmask idea is simple. The real work is writing a correct LCA with binary lifting and a clean traversal. Expect most bugs there, not in the palindrome check.

Do I need recursion or iteration for the tree traversal?+

Iterative is safer. A tree with n nodes can be a long chain, and deep recursion can overflow the stack in some languages. Use a stack or queue from root 0 to fill parent, depth, and prefix mask.

How do I prepare for this in 48 hours?+

Write three things from memory: prefix XOR masks on a tree, binary lifting LCA, and the popcount-at-most-one check. Then run both examples by hand, including the single-node query [1,1], which must return 1.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Uber.

OA at Uber?
Invisible during screen share
Get it