Path Leaf Character Iterator
Updated
Problem statement
Read the strings at the ends of the given tree paths, one character at a time.
Only leaf nodes (nodes with no children) store strings. You are given a list of paths, each ending at a leaf. Read those leaf strings in the order the paths appear.
How the iterator works
Implement an Iterate class with a next() method. Each call returns the next single character:
- Read the current leaf string from left to right.
- When that string is finished, continue with the next path. Skip any empty leaf strings.
- Once all strings have been read, return
""on this call and every later call.
For example, if the paths lead to "ab" and then "XYZ", consecutive calls return "a", "b", "X", "Y", "Z", and then "". Each call continues where the previous one stopped. A space counts as a character too.
How to read the input
- Nodes are numbered from
0tonodeValues.length - 1. In a nonempty tree, node0is the root. nodeValues[i]holds the string at leafi. Internal nodes use""as a placeholder.- Each row of
pathslists the nodes on one path from the root to a leaf. Its last number tells you which leaf string to read. For example,[0, 2]points tonodeValues[2]. - Follow the order in
paths, even if it differs from node-number order. If a path appears again, read its leaf string again.
The paths are already provided; you do not need to build or search the tree.
Collect the results
Complete iterateLeafCharacters(nodeValues, paths, callCount). Create one iterator, call next() exactly callCount times, and return the results in an array. For example, callCount = 5 means five calls and five results. If callCount = 0, return [].
Function
iterateLeafCharacters(nodeValues: String[], paths: int[][], callCount: int) → String[]Examples
Example 1
nodeValues = ["","ab","XY"]paths = [[0,2],[0,1]]callCount = 5return = ["X","Y","a","b",""]The supplied paths visit leaf 2 before leaf 1. The first four calls emit X, Y, a, and b. The fifth call finds the iterator exhausted.
Example 2
nodeValues = ["","","q"]paths = [[0,1],[0,2],[0,2]]callCount = 3return = ["q","q",""]The empty value at leaf 1 is skipped. Leaf 2 occurs twice, so its character is emitted twice. The final call returns the empty string.
Example 3
nodeValues = ["","cat","dog"]paths = [[0,1],[0,2]]callCount = 2return = ["c","a"]Both calls read from the first leaf. The iterator has not reached the last character of cat or the next path.
Constraints
0 <= nodeValues.length <= 100000.0 <= paths.length <= 100000and0 <= callCount <= 100000.- Each path is nonempty. The total number of node indices across all paths is at most
200000. - All path indices are valid. The paths are consistent root-to-leaf paths in one fixed rooted tree. A leaf never occurs as an internal node of a path.
- If
nodeValuesis empty,pathsis empty. A path containing only the root is valid when the root is a leaf. - Every value contains only English letters, digits, and spaces. The sum of all stored string lengths is at most
100000. Empty leaf values are allowed.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Hybrid Run-Length and Bit-Packing EncoderDatabricks · ONSITE INTERVIEWDatabricks interview questions
- Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Stateful Log Transform and SearchRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Digit-Coded Card ValidationCapital One · ONSITE INTERVIEWCapital One interview questions
- Query JSON Records with WHERE and ORDER BYGitHub · ONSITE INTERVIEW
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW