Databricks interview question

Leetcode type, with runtime follow up.