Samsara Interview Question

Leetcode med/hard and then to optimize solution.