FastPrepTranslate Values in a Nested Map
Problems/Translate Values in a Nested Map

Translate Values in a Nested Map

Zip logoZip● MediumFULLTIMENEW GRADPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The reports preserve all-or-nothing validation, nested-map recursion, memoized converter calls, and an explicit invalid _id suffix rule. The tree-array adapter and translation lookup table are practice mechanics; the judged core task matches the source at about 88%.

A nested map is represented as a rooted tree. Node 0 is the root. For each other node, parent[i] is its containing map node. A node with isLeaf[i] == true stores the string values[i]; a map node stores the empty string.

The supplied translations array contains [source, translated] pairs and represents an expensive converter. A leaf value is invalid when it ends with _id.

  • If any leaf value is invalid, return the original values array unchanged.
  • Otherwise, translate every leaf while keeping every map-node value unchanged.
  • Repeated equal leaf values must reuse one converter lookup.

Return the resulting values in the original node order.

Function

translateNestedValues(parent: int[], isLeaf: boolean[], values: String[], translations: String[][]) → String[]

Examples

Example 1

parent = [-1,0,0]isLeaf = [false,true,true]values = ["","hello","world"]translations = [["hello","hola"],["world","mundo"]]return = ["","hola","mundo"]

Both leaf values are valid, so both are translated. The root map node remains empty.

Example 2

parent = [-1,0,1,1]isLeaf = [false,false,true,true]values = ["","","hello","hello"]translations = [["hello","hola"]]return = ["","","hola","hola"]

The repeated leaf value is looked up once and reused for both nested leaves.

Example 3

parent = [-1,0,0]isLeaf = [false,true,true]values = ["","hello","account_id"]translations = [["hello","hola"],["account_id","cuenta"]]return = ["","hello","account_id"]

Because one leaf ends with _id, the operation is atomic and returns every original value.

Constraints

  • 1 <= parent.length == isLeaf.length == values.length <= 100000.
  • parent[0] == -1, and for i > 0, 0 <= parent[i] < i.
  • Map nodes have an empty value; leaf nodes have a non-empty value.
  • 0 <= translations.length <= 100000, and source strings are distinct.
  • Every distinct valid leaf value appears in translations.
  • The combined length of all strings is at most 10^6.

More Zip problems

  • Maximum Survival Time in a Graph PursuitPHONE SCREEN · Seen Sep 2026
  • Find All Zip Rummy MeldsPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2026
  • Shortest Path Around Moving ParadesPHONE SCREEN · Seen Mar 2026
All Zip interview questionsSee Zip hiring insights
Saved
class Solution {
    public String[] translateNestedValues(int[] parent, boolean[] isLeaf, String[] values, String[][] translations) {
        // Write your code here.
    }
}
Checking account…