commented solution with test cases