Datadog Interview Question

They ask me to solve on midium-level leet code exercise.