Databricks interview question

Standard algorithm question.