ProblemsModern C++Safe config reader
IntermediateModern C++

Safe config reader

Context

The product config has been edited for five years by anyone passing by: six levels of nesting, half the keys optional, zero docs. The current reader is a staircase of if (node != nullptr) and still crashes on staging every Thursday. The PM says 'just make it not crash'. std::optional is your way off the staircase.

Task

Implement getConfigValue — a function that reads a nested config value by dot-separated path, returning std::nullopt if any node in the chain is missing. No exceptions, no raw null checks scattered around.

Constraints

  • Must use std::optional throughout
  • Must not throw
  • No raw pointer chains
  • Empty path ("") returns the root's value if it has one

Before you code

  • How does std::optional model "value or nothing"?
  • What's the difference between optional::value() and optional::value_or()?
  • How would you chain optional lookups without nested ifs?

Tests

  • #1Happy path — existing deep key
  • #2Missing intermediate node returns nullopt
  • #3Missing leaf returns nullopt
  • #4Empty path returns root value

Hints

Hint 1

Split the path on . and walk the children map level by level.

Hint 2

At each step, if the key is missing return std::nullopt; at the end return the node's value.

Editoroptional-chain.cpp
Results

Hit Submit (or ⌘/Ctrl + ↵) — test results will show up here.