Palindrome Number

Asked atInfosys
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below

The problem

You are given an integer n. You need to check whether the number is a **palindrome **number or not. Return **true **if it's a **palindrome **number, otherwise return false.

A **palindrome **number is a number which reads the **same **both left to right and right to left.

Input: n = 121 Output: true Explanation: When read from left to right : 121. When read from right to left : 121.

Input: n = 123 Output: false Explanation: When read from left to right : 123. When read from right to left : 321.

Input: 101

  • 0 <= n <= 5000
  • n will contain no leading zeroes except when it is 0 itself.

cpp

class Solution {
public:
    bool isPalindrome(int n) {

    }
};

java

class Solution {
    public boolean isPalindrome(int n) {

    }
}

python

class Solution:
    def isPalindrome(self, n):

javascript

class Solution {
    isPalindrome(n) {

    }
}

csharp

public class Solution
{
    public bool IsPalindrome(int n)
    {

    }
}

go

func isPalindrome(n int) bool {
   
}
Stuck? Show a way to structure it+
  1. 01Reject negative inputs if the domain includes them.
  2. 02Reverse either all digits or only the second half.
  3. 03Compare the original with the reversed representation.
  4. 04Handle trailing zeroes correctly.

Reference answer

Then expect these follow-ups

  • How would you test palindrome without converting to a string?

    Tests: numeric manipulation

  • How would you check a linked-list palindrome?

    Tests: list algorithms

Hiring now

Get the Infosys interview, not just the prep

landed applies to roles like these for you, on each company’s own site, with answers written for the role.

Get 5x interviews10 free auto-applies. No card.
Or practice this question first

Next question