FastPrepPath Leaf Character Iterator

Path Leaf Character Iterator

Google logoGoogle● MediumINTERNONSITE INTERVIEW

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.

The problem statement continues
Pro

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.

FastPrep Pro
Reported in 1 Google interview this week

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
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week
See Google hiring insights
CodePython 3
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Google interview this week

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
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week