IBM Interview Question

Magic Carpet question that was easy Leetcode question.