Samsara interview question

Leetcode med/hard and then to optimize solution.