Doubt in execution of a function

Can you please tell where am I going wrong in this question?

your code lacks at several points

refer this code
i have added step wise comments all corner cases are talked about in the code
u`ll understand where u have gone wrong