← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Uber coding round for a Software Engineer role. One problem, tree-based, and it was a variant of a well-known palindrome path problem. Harder than it looks if you haven't seen bitmask tricks before.

Questions Asked (1)

Q1

Given a tree of n nodes where each edge has a single lowercase letter label, find the number of unordered node pairs (u, v) such that the letters along the path between them can be rearranged to form a palindrome.

Algorithms & Data Structures
Author's notes

The palindrome condition is the easy part to state: at most one character with an odd frequency.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a bitmask to represent the parity of letter counts along the path from the root to each node. For each node, compute the mask via DFS; then count pairs of nodes whose masks differ by at most one bit (i.e., XOR is 0 or a power of two). Use a hash map to count frequencies of masks and their one-bit variants to achieve O(n * 26) time.

Pro tip: Emphasize that the palindrome condition depends only on parity of character counts, not order. Mention that using a 26-bit integer mask is efficient and that the counting step can be optimized by iterating over 26 possible bit flips.

1. Understand the palindrome condition

A string can be rearranged into a palindrome if and only if at most one character has an odd count. So for a path, we need the parity of each letter's count.

2. Represent parity as a bitmask

Assign each letter a bit position (0-25). For any path, the parity mask is the XOR of masks of edges along the path. For root-to-node paths, compute mask via DFS.

3. Relate path mask to root masks

For any two nodes u and v, the mask of the path between them is mask[u] XOR mask[v]. So we need pairs where mask[u] XOR mask[v] has at most one bit set.

4. Count valid pairs efficiently

Use a hash map to count frequencies of each mask. For each node, add counts of masks that differ by 0 or 1 bit (i.e., same mask or mask XOR (1<<i) for i=0..25). Accumulate pairs.

5. Analyze complexity and edge cases

Time O(n * 26) and space O(n). Handle n=1 (no pairs) and ensure unordered pairs are counted once (e.g., by iterating and adding before inserting current node's mask).

Key Points to Mention

  • Palindrome rearrangement condition: at most one character with odd frequency.
  • Bitmask representation of parity for 26 lowercase letters.
  • Path mask = XOR of root-to-u and root-to-v masks.
  • Counting pairs using hash map and checking 0 or 1 bit differences.
  • Time complexity O(26 * n) and space O(n).
  • Avoid double-counting by processing nodes in order and using frequency map.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.