This question:
… got closed for this reason:
Code not implemented or not working as intended …
… as the author wrote:
However, on hackerrank it says the wrong answer even though it expects the same result. Any idea what's going on?
And yes I can see that this is the OP's main concern.
However, the code is working as intended. So the answer to this question is: "It seems to be a problem on the external website".
When this part of the question is edited or removed, it has a solid problem description and a working function for review.
So my question is: Shouldn't this be re-open and edited instead of getting closed?