Solve two online assessment problems
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Take-home Project
Quick Answer: This two-part prompt evaluates algorithmic problem-solving skills: the first problem tests optimization under index-based movement and number-theoretic constraints (prime indices ending with digit 3), while the second assesses tree traversal and multiset/path frequency reasoning for palindrome-anagram checks on rootward paths.
Part 1: Maximize Skipped Positions
Constraints
- 1 <= n <= 10^6
- Indices are 0-based: 0 to n - 1
- A jump destination must be prime and must end with digit 3
Examples
Input: (1,)
Expected Output: 0
Explanation: You start at the last index already, so no movement is needed.
Input: (3,)
Expected Output: 0
Explanation: The only indices after 0 are 1 and 2; neither is a prime ending in 3.
Input: (4,)
Expected Output: 2
Explanation: Index 3 is prime and ends with 3, so jump directly from 0 to 3 and skip indices 1 and 2.
Input: (20,)
Expected Output: 12
Explanation: Valid jump destinations below 20 are 3 and 13. Jumping directly from 0 to 13 gives 12 points, which is optimal.
Input: (44,)
Expected Output: 42
Explanation: Index 43 is prime and ends with 3. Jump from 0 to 43 for 42 points, then you are already at the last index.
Hints
- If you write the total score for several jumps in a row, most terms cancel out.
- Because you can jump to any valid later index directly, ask whether making extra jumps can ever improve the score.
Part 2: Count Palindromic-Anagram Ancestors in a Rooted Tree
Constraints
- 1 <= tree_nodes <= 2 * 10^5
- 0 <= len(queries) <= 2 * 10^5
- arr[i] is a lowercase English letter
- edges forms a valid tree rooted at node 0
Examples
Input: (1, ['a'], [], [0])
Expected Output: [1]
Explanation: The only path segment is ['a'], which is already a palindrome.
Input: (4, ['z', 'a', 'a', 'a'], [(0, 1), (0, 2), (1, 3)], [3])
Expected Output: [3]
Explanation: For query node 3, the segments 3->3, 3->1, and 3->0 all have at most one odd character count.
Input: (3, ['a', 'b', 'a'], [(0, 1), (1, 2)], [2, 1, 0])
Expected Output: [2, 1, 1]
Explanation: For node 2, segments 'a' and 'aba' work. For node 1, only 'b' works. For node 0, only 'a' works.
Input: (5, ['a', 'b', 'b', 'a', 'c'], [(0, 1), (0, 2), (1, 3), (1, 4)], [3, 4, 2])
Expected Output: [2, 1, 1]
Explanation: Query 3 has valid segments 'a' and 'aba'. Query 4 has only 'c'. Query 2 has only 'b'.
Input: (4, ['a', 'a', 'b', 'b'], [(0, 1), (1, 2), (2, 3)], [3, 3])
Expected Output: [4, 4]
Explanation: All four ancestor segments for node 3 can be rearranged into palindromes, and the query is repeated.
Hints
- A string can be permuted into a palindrome iff at most one character count is odd. Represent odd/even counts with a 26-bit mask.
- During a DFS from the root, maintain counts of prefix masks along the current root-to-node path so each query can be answered from the current path state.