Check whether a given string or number is a palindrome. A palindrome is a sequence that remains the same when read from both forward and backward directions.

Approach: The solution uses the two-pointer technique for strings. One pointer starts from the beginning and the other from the end of the input. Characters at both positions are compared while moving the pointers toward the center. If any mismatch is found, the input is not a palindrome.

For numbers, the integer is first converted into a string and the same comparison logic is applied. This approach keeps the implementation simple and reusable for both strings and numeric values.

Java Solution:

import java.util.Scanner;

public class PalindromeCheck {

    public static boolean isPalindrome(String input) {
        int left = 0;
        int right = input.length() - 1;

        while (left < right) {
            if (input.charAt(left) != input.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }

        return true;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a string or number: ");
        String input = scanner.nextLine();

        if (isPalindrome(input)) {
            System.out.println(input + " is a Palindrome.");
        } else {
            System.out.println(input + " is not a Palindrome.");
        }

        scanner.close();
    }
}

Time Complexity: O(n), where n is the length of the input. In the worst case, each character is compared only once.

Space Complexity: O(1), as the algorithm uses only a few additional variables regardless of input size.

Key Interview Points: • The two-pointer approach avoids creating a reversed copy of the input and is memory efficient. • The same logic works for both strings and numbers by converting numeric input to String format. • An alternative approach is to reverse the string using StringBuilder.reverse() and compare it with the original value, but that requires additional memory.