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.
An online assessment contained two coding problems:
n
indexed from
0
to
n - 1
. Start at index
0
and reach index
n - 1
.
At any index
i
, you may:
i + 1
, if it exists; or
j > i
such that
j
is a prime number and the decimal representation of
j
ends with
3
.
i
to
j
, you gain
j - i - 1
points, equal to the number of positions skipped. Moving one step to the right gives
0
points.
Compute the maximum total score achievable when you reach the last index.
tree_nodes
nodes is rooted at node
0
. The nodes are numbered from
0
to
tree_nodes - 1
. Each node
i
stores a lowercase character
arr[i]
.
You are also given an array
queries
of length
m
. For each query node
q = queries[i]
, consider every node
v
on the path from
q
to the root, including
q
itself. Count how many such nodes
v
have the property that the characters on the path from
q
up to
v
can be rearranged to form a palindrome.
Return an integer array of length
m
, where the
i
-th value is the answer for
queries[i]
.
Example:
tree_nodes = 4
arr = ['z', 'a', 'a', 'a']
(0, 1)
,
(0, 2)
,
(1, 3)
queries = [3]
[3]
Explanation: For query node
3
, the path segments ending at nodes
3
,
1
, and
0
all have character counts that can be rearranged into a palindrome.