Pergunta de entrevista da empresa Okta

Leetcode medium question, solvable within 30 minutes or so.