Siemens interview question

Leetcode based coding to solve