Check palindrome and find next larger palindrome
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in string and numeric manipulation, specifically palindrome detection and generation of the next strictly larger numeric palindrome from a large integer represented as a string.
Valid Palindrome (Case-Sensitive)
Constraints
- 1 <= len(s) <= 2 * 10^5
- s contains only alphanumeric characters
- Comparison is case-sensitive
Examples
Input: ("racecar",)
Expected Output: True
Explanation: Reads identically in both directions.
Input: ("ab",)
Expected Output: False
Explanation: 'ab' reversed is 'ba', which differs.
Input: ("a",)
Expected Output: True
Explanation: A single character is trivially a palindrome.
Input: ("abba",)
Expected Output: True
Explanation: Even-length palindrome.
Input: ("Aa",)
Expected Output: False
Explanation: Case-sensitive: 'A' != 'a', so not a palindrome.
Input: ("12321",)
Expected Output: True
Explanation: Numeric string that mirrors around the center '3'.
Input: ("abca",)
Expected Output: False
Explanation: First char 'a' matches last 'a', but 'b' != 'c'.
Hints
- A string is a palindrome iff it equals its own reverse.
- For O(1) extra space, walk two pointers from both ends toward the center and compare characters; stop at the first mismatch.
- Case-sensitive means 'A' != 'a' — do NOT lowercase before comparing.
Next Larger Palindrome Number (Big Integer)
Constraints
- 1 <= len(x) <= 2 * 10^5
- x has no leading zeros unless x == "0"
- x may exceed 64-bit range — process it as a string, never as a native integer
Examples
Input: ("123",)
Expected Output: "131"
Explanation: Mirror left '1','2' over -> '121'; not > 123, so bump middle 2->3 giving '131'.
Input: ("99",)
Expected Output: "101"
Explanation: All 9s overflow to a longer palindrome 101.
Input: ("808",)
Expected Output: "818"
Explanation: Mirror gives 808 (==x, not strictly greater); bump middle 0->1 -> 818.
Input: ("9",)
Expected Output: "11"
Explanation: Single all-9s digit rolls over to 11.
Input: ("0",)
Expected Output: "1"
Explanation: Mirror gives 0 (not > 0); bump middle 0->1 -> 1.
Input: ("1",)
Expected Output: "2"
Explanation: Smallest palindrome strictly greater than 1 is 2.
Input: ("11",)
Expected Output: "22"
Explanation: Mirror gives 11 (==x); bump to 22.
Input: ("19",)
Expected Output: "22"
Explanation: Mirror left '1' -> '11' which is < 19; increment with outward carry -> 22.
Input: ("1234",)
Expected Output: "1331"
Explanation: Mirror -> 1221 (< 1234); bump inner pair 2->3 -> 1331.
Input: ("129",)
Expected Output: "131"
Explanation: Mirror -> 121 (< 129); bump middle 2->3 -> 131.
Input: ("100",)
Expected Output: "101"
Explanation: Mirror -> 101 which is > 100, so it is the answer directly.
Input: ("999",)
Expected Output: "1001"
Explanation: All 9s overflow to 1001.
Input: ("12921",)
Expected Output: "13031"
Explanation: Mirror -> 12921 (==x); bump middle 9->10 carries outward: 12921 -> 13031.
Hints
- Mirror the left half of the digits onto the right half to form a candidate palindrome of the same length.
- If that mirrored candidate is strictly greater than x, it is the answer. Compare as equal-length strings (lexicographic == numeric here).
- Otherwise, increment the middle digit(s) by 1 and propagate the carry outward symmetrically, then the number is still a palindrome and is now larger.
- Special case: a string of all 9s (e.g. '9', '99', '999') rolls over to 1 followed by (n-1) zeros followed by 1 (e.g. '11', '101', '1001').