MathWorks interview question

Leetcode question, two technical interview with a total about 4.