Problem · String

Parse Indented YAML Mappings

Learn this problem
MediumUber logoUberFULLTIMEPHONE SCREEN
See Uber hiring insights

Problem statement

Parse a valid YAML-like document yaml containing nested mappings. Each non-empty line has one of these forms:

  • key: opens a nested mapping;
  • key: value defines a scalar value.

Indentation uses exactly two spaces per level. A child line is indented one level deeper than its containing mapping; indentation may later return to any earlier open level. Keys contain only letters, digits, underscores, and hyphens, and sibling keys are unique. Scalar values are non-empty trimmed text; quotes and # have no special meaning in this subset. Empty lines are ignored.

Return one string for each scalar line in document order. Use its full dot-separated key path, followed by = and the trimmed scalar value.

Function

parseYamlMappings(yaml: String) → String[]

Examples

Example 1

yaml = "server:\n  host: api.example.com\n  port: 443\nmode: prod"return = ["server.host=api.example.com","server.port=443","mode=prod"]

The two indented scalar keys belong under server; mode returns to the root level.

Example 2

yaml = "app:\n  database:\n    host: db\n    enabled: true\n  name: demo"return = ["app.database.host=db","app.database.enabled=true","app.name=demo"]

The path stack grows through app.database and then returns one level for app.name.

Constraints

  • 1 <= yaml.length <= 2 * 10^5.
  • The document contains at most 2 * 10^4 non-empty lines.
  • The document satisfies the grammar and indentation rules stated above.

More Uber problems

drafts saved locally
public String[] parseYamlMappings(String yaml) {
    // Write your code here.
}
yaml"server:\n host: api.example.com\n port: 443\nmode: prod"
expected["server.host=api.example.com", "server.port=443", "mode=prod"]
checking account