FastPrepPath Leaf Character Iterator
Problems/Path Leaf Character Iterator

Path Leaf Character Iterator

Google logoGoogle● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Our version is about 90% the same as the reported question. The report says to read leaf strings one character at a time with next(), but leaves out the input format and edge cases. For online practice, nodeValues holds the strings and paths gives their reading order. Empty strings are skipped, repeated paths are read again, and next() returns "" once everything has been read. callCount just tells you how many times to call next() and collect the results: 5 means five calls. These input and edge-case rules, along with the character limits and examples, are our choice and not requirements in the report. Updated 10-19-2026: reworded the problem to make it easier to understand.

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:

  1. Read the current leaf string from left to right.
  2. When that string is finished, continue with the next path. Skip any empty leaf strings.
  3. 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 0 to nodeValues.length - 1. In a nonempty tree, node 0 is the root.
  • nodeValues[i] holds the string at leaf i. Internal nodes use "" as a placeholder.
  • Each row of paths lists 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 to nodeValues[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 <= 100000 and 0 <= 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 nodeValues is empty, paths is 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

Interview reports that mention this question

  • Google Intern Team Matching: 14 Experiences (Part 4 of 8)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public String[] iterateLeafCharacters(String[] nodeValues, int[][] paths, int callCount) {
        // write your code here
    }
}
Checking account…