Compass interview question

Similar like Leetcode question 139, 140