Solve jump maximization and palindromic path queries
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Take-home Project
You are given **two independent algorithmic problems**.
---
## Problem 1 — Jump game with special destinations (maximize score)
You are given an integer array `arr` of length `n` (0-indexed). You start at index `0` and must end exactly at index `n-1`.
- Your **score** is the sum of `arr[i]` over all visited indices (including start and end).
- From an index `i`, you may jump to:
1. `i + 1` (if `i + 1 < n`), **or**
2. any index `j` such that `j > i` and `j` is a positive integer whose **ones digit is 3** (i.e., `j ∈ {3, 13, 23, 33, ...}`) and `j < n`.
It is guaranteed that `n-1` is reachable.
**Task:** Return the **maximum possible score** achievable.
**Input:** `int[] arr`
**Output:** `long` (or `int` if you can prove no overflow)
---
## Problem 2 — Count ancestor endpoints forming a palindrome (tree queries)
You are given a rooted tree with root node `0`.
- `treeNodes`: number of nodes `N` (nodes are `0..N-1`)
- `nodes`: `char[]` of length `N`, where `nodes[i]` is the lowercase letter (`'a'..'z'`) stored **on node `i`**
- `nodeFrom`, `nodeTo`: integer arrays of length `N-1` describing directed edges `nodeFrom[k] -> nodeTo[k]` (parent to child). This forms a valid tree.
- `queries`: integer array of query nodes.
For each query node `u = queries[t]`, consider the unique path from `u` up to the root `0`.
For every endpoint `v` on this path (where `v` is an ancestor of `u`, including `u` itself), form the multiset/string of characters on the nodes along the path from `u` to `v` (inclusive). The characters may be **rearranged arbitrarily**.
A path `u -> ... -> v` is counted if its characters can be rearranged into a palindrome (equivalently: at most one character has an odd frequency).
**Task:** For each query node `u`, return the **number of ancestors `v` (including `u`)** such that the path from `u` to `v` can be rearranged into a palindrome.
**Output:** `int[] res` where `res[t]` answers `queries[t]`.
### Example
```
N = 4
nodes = [ 'z', 'a', 'a', 'a' ]
nodeFrom = [0, 0, 1]
nodeTo = [1, 2, 3]
queries = [3]
```
Tree edges: `0->1`, `0->2`, `1->3`. Query `u=3` has path to root: `3(a) -> 1(a) -> 0(z)`.
Valid endpoints:
- `v=3`: "a" (palindrome)
- `v=1`: "aa" (palindrome)
- `v=0`: "aaz" can be rearranged to "aza" (palindrome)
So the output is `[3]`.
---
### Constraints (assume typical OA scale)
You should design an algorithm that is efficient for large `n`/`N`/`|queries|` (e.g., up to `10^5` or more).
Quick Answer: This pair of problems evaluates algorithmic problem-solving skills: the first assesses array-based path optimization and stateful score maximization under constrained jumps, while the second assesses tree traversal combined with character-frequency parity checks for ancestor-path palindromic queries.
Maximum Score with Special Jumps
Given scores, start at index 0. From i you may move to i+1 or jump to any later index whose decimal index ends in 3. Return the maximum collected score.
Constraints
- Inputs are provided as Python literals compatible with the function signature.
- Return a deterministic value exactly matching the requested output.
Examples
Input: ([10, -100, -100, 5, 1],)
Expected Output: 16
Explanation: Jump to index 3 avoids negatives.
Input: ([5, 1, 100, 2, 3],)
Expected Output: 111
Explanation: Walking is better.
Input: ([],)
Expected Output: 0
Explanation: Empty path.
Input: ([4, -5, -5, 6, -100, 10, 1, 1, 1, 1, 2, 2, 2, 50],)
Expected Output: 60
Explanation: Later special index.
Hints
- Start with a direct data structure representation.
- Handle edge cases before the main loop.
Palindromic Ancestor Paths
For each queried node in a rooted directed tree, count ancestors whose path to that node can be rearranged into a palindrome.
Constraints
- Inputs are provided as Python literals compatible with the function signature.
- Return a deterministic value exactly matching the requested output.
Examples
Input: (5, [[0, 1], [1, 2], [1, 3], [3, 4]], 'ababa', [4, 2, 0])
Expected Output: [3, 2, 1]
Explanation: Three query nodes.
Input: (4, [[0, 1], [0, 2], [2, 3]], 'aaaa', [3, 1])
Expected Output: [3, 2]
Explanation: All same labels.
Input: (1, [], 'z', [0])
Expected Output: [1]
Explanation: Single node.
Hints
- Start with a direct data structure representation.
- Handle edge cases before the main loop.