HackerRank 'Cats And A Mouse' Solution

Martin Kysel · July 26, 2020

Short Problem Definition:

Two cats and a mouse are at various positions on a line. You will be given their starting positions. Your task is to determine which cat will reach the mouse first, assuming the mouse doesn’t move and the cats travel at equal speed. If the cats arrive at the same time, the mouse will be allowed to move and it will escape while they fight.

Cats and a Mouse

Complexity:

time complexity is O(N)

space complexity is O(1)

Execution:

This one is trivial.

Solution:
def catAndMouse(x, y, z):
    catA = abs(x-z)
    catB = abs(y-z)
    if (catA < catB):
        return "Cat A"
    elif (catB < catA):
        return "Cat B"
    else:
        return "Mouse C"

Twitter, Facebook

To learn more about solving Coding Challenges in Python, I recommend these courses: Educative.io Python Algorithms, Educative.io Python Coding Interview.