Datadog interview question

Implement a recursive algorithm to satisfy the required outcome.