We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Cats and a Mouse
  5. Discussions

Cats and a Mouse

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 1055 Discussions, By:

recency

Please Login in order to post a comment

  • thaliahuynh
    14 hours ago+ 0 comments

    Python

    def catAndMouse(x, y, z):
        a_dist = abs(x-z)
        b_dist = abs(y-z)
        if a_dist == b_dist:
            return 'Mouse C'
        elif a_dist > b_dist:
            return 'Cat B'
        else: 
            return 'Cat A'
    
    0|
    Permalink
  • buludlumoka
    1 week ago+ 0 comments

    C#

            int CatADistance = x > z ? x - z : z - x; 
            int CatBDistance = y > z ? y - z : z - y; 
            if (CatADistance < CatBDistance) return "Cat A";
            else if (CatADistance > CatBDistance) return "Cat B";
            else return "Mouse C";
    
    0|
    Permalink
  • oakcy01
    1 week ago+ 0 comments

    Easy C# Solution

       static string catAndMouse(int x, int y, int z) {
    
                int catADistance = 0;
                int catBDistance = 0;
                
                if(x < z)
                {
                    catADistance = z - x;
                }
                if(y < z)
                {
                    catBDistance = z - y;
                }
                if(x > z)
                {
                    catADistance = x - z;
                }
                if(y > z)
                {
                    catBDistance = y - z;
                }
                
                if(catADistance > catBDistance)
                {
                    return "Cat B";
                }
                if(catBDistance > catADistance)
                {
                    return "Cat A";
                }
                else
                {
                    return "Mouse C";
                }
            
    
    0|
    Permalink
  • reddy06112000
    2 weeks ago+ 0 comments
    def catAndMouse(x, y, z):
        i,j= abs(z-x),abs(z-y)
        if i==j:
            return 'Mouse C'
        if i<j:
            return 'Cat A'
        else:
            return 'Cat B'
    
    0|
    Permalink
  • mrjackyliang
    2 weeks ago+ 0 comments

    JavaScript solution:

    /**
     * x - cat a
     * y - cat b
     * z - mouse
     */
    function catAndMouse(x, y, z) {
        const catA = Math.abs(x - z);
        const catB = Math.abs(y - z);
        
        console.log(catA, catB);
        
        if (catA === catB) {
            return 'Mouse C';
        } else if (catA < catB) {
            return 'Cat A';
        } else if (catA > catB) {
            return 'Cat B';
        }
    }
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy