Path Leaf Character Iterator
Problem statement
Implement an Iterate class for a fixed sequence of paths in a rooted tree. Only leaf nodes store string values. Each call to next() emits the next character from the terminal leaf of the current path. Finish that leaf's value before moving to the next path in the supplied sequence.
Path and value representation
The tree's nodes are indexed from 0 through nodeValues.length - 1, with root 0 when the tree is nonempty. nodeValues[i] is the string at leaf i; internal nodes use an empty-string placeholder. The paths are already supplied, so you do not need to discover them or construct a tree.
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.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 2 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.