Placement Prep

Program to Find the Largest Palindrome in an Array

Step-by-step walkthrough to find the largest palindrome in an array of integers, with Python code, two worked examples, and complexity analysis.

By FACE Prep Team 5 min read
palindrome arrays algorithm placement-prep python string-manipulation coding-interview

Given an array of non-negative integers, finding the largest palindrome is a single-pass problem that tests one core skill: checking whether a number reads the same forwards and backwards.

What Is a Palindrome?

A palindrome is a number (or string) that reads identically in both directions. Some examples:

  • Numeric palindromes: 5, 131, 54545, 984565489
  • Not palindromes: 25, 61, 1111118
  • String palindromes: “Radar”, “Madam”, “racecar”

Single-digit integers (0 through 9) are always palindromes. There is only one digit to read in either direction, so the forward and backward readings are identical by definition.

The palindrome check for a number works like this: convert the number to a string, reverse the string, and compare. If the original and reversed strings match, the number is a palindrome. The Python documentation on string sequences describes the [::-1] slice notation that makes this a one-step operation.

Problem Statement and Constraints

Given: An array of non-negative integers.

Find: The element with the highest numeric value that is also a palindrome.

Return: -1 if no palindrome exists in the array.

Constraints to keep in mind:

  • Elements are non-negative (0 and above). Negative numbers are not in scope.
  • “Largest” means highest numeric value, not longest digit count. Between 131 and 54545, the answer is 54545.
  • An empty array returns -1.

Two test cases from the problem’s documented history:

  • Test case 1: Input = [5, 131, 54545, 1111118] — Output = 54545
  • Test case 2: Input = [2, 4, 5, 8, 25, 61] — Output = 8

Both are verified below before we trust them.

Step-by-Step Algorithm

The approach is a linear scan with a running maximum:

  1. Set result = -1. This initialises the “no palindrome found” return value automatically.
  2. For each element in the array:
    • Check whether it is a palindrome (convert to string, compare with its reverse).
    • If it is a palindrome and greater than result, update result.
  3. After the loop ends, return result.

No sorting is needed. The running maximum means the answer is found in a single pass.

Worked Example 1: [5, 131, 54545, 1111118]

  • Element 5: "5" reversed = "5". Match. result updates to 5.
  • Element 131: "131" reversed = "131". Match. 131 > 5. result updates to 131.
  • Element 54545: "54545" reversed = "54545". Match. 54545 > 131. result updates to 54545.
  • Element 1111118: "1111118" reversed = "8111111". No match. result stays at 54545.
  • Return value: 54545. ✓

Worked Example 2: [2, 4, 5, 8, 25, 61]

  • Element 2: "2" reversed = "2". Match. result updates to 2.
  • Element 4: "4" reversed = "4". Match. 4 > 2. result updates to 4.
  • Element 5: "5" reversed = "5". Match. 5 > 4. result updates to 5.
  • Element 8: "8" reversed = "8". Match. 8 > 5. result updates to 8.
  • Element 25: "25" reversed = "52". No match. result stays at 8.
  • Element 61: "61" reversed = "16". No match. result stays at 8.
  • Return value: 8. ✓

Both test cases check out.

Python Implementation

The s[::-1] slice produces the full reverse of string s in Python, making the palindrome check a single comparison.

def is_palindrome(n):
    s = str(n)
    return s == s[::-1]

def largest_palindrome_in_array(arr):
    result = -1
    for num in arr:
        if is_palindrome(num) and num > result:
            result = num
    return result

# Test case 1
print(largest_palindrome_in_array([5, 131, 54545, 1111118]))  # Output: 54545

# Test case 2
print(largest_palindrome_in_array([2, 4, 5, 8, 25, 61]))     # Output: 8

The same logic applies in Java (using new StringBuilder(str).reverse().toString()) and in C++ (using std::string and std::reverse from the <algorithm> header). Python’s s[::-1] is the most concise form; the underlying comparison is language-agnostic.

GeeksforGeeks covers the mathematical reversal approach, building the reverse digit by digit without string conversion. That approach is useful when string operations are restricted, though in most placement environments there is no such restriction.

Complexity Analysis

  • Time complexity: O(n x d), where n is the number of elements and d is the digit count of the largest element. The outer loop runs n times. Each palindrome check converts a number to a string of d characters and compares it with its reverse, which is also d characters. For 32-bit integers, d is at most 10.

  • Space complexity: O(d). The string representation of each number occupies d characters temporarily. No additional data structure — no stack, no hash map — is needed.

The linear scan with a running maximum is optimal for this problem. Sorting the array first would add O(n log n) overhead with no benefit, since you still need to scan all elements to identify palindromes.

Common Pitfalls and Edge Cases

Four edge cases trip up most first attempts. Account for each one before submitting in a timed round.

  • Empty array. Initialising result to a sentinel value (such as -1 for non-negative arrays, or a None equivalent in Python) lets the function signal the absence of any palindrome cleanly. Returning 0 here is a quiet bug: 0 is itself a single-digit palindrome, so a 0-returning function cannot distinguish ‘no palindromes found’ from ‘the only palindrome is 0.’
  • Negative numbers. Most placement-problem statements scope to non-negative integers, but recruiters sometimes test whether candidates ask about this. The string ‘-121’ is not the same as its reverse ‘121-’. If negative numbers are in scope, check the absolute value or skip negatives entirely; clarify the assumption in your code comment.
  • Single-element arrays. A one-element array containing a palindrome should return that element. A one-element array containing a non-palindrome should return the sentinel. The base loop handles both cases correctly only if the running maximum is initialised below every valid array value, which is why a sentinel value is safer than initialising to arr[0].
  • All-non-palindrome arrays. The function should return the sentinel, not the largest element. Test against arrays like [23, 45, 67] before declaring done.

One additional consideration appears in interview follow-ups: how does your solution behave when the array is very large (10^6 or 10^7 elements)? The O(n x d) bound still holds, but the constant factor of string conversion in Python is non-trivial. A purely arithmetic palindrome check (reversing digits via integer arithmetic) often runs 3 to 5 times faster on large inputs. Interviewers value candidates who can articulate this trade-off even if the implementation stays string-based.

Where This Problem Appears in Placements

The problem has appeared in Oracle’s on-campus coding rounds as a warm-up question. It tests whether a candidate can:

  1. Recognise the string-representation pattern (convert integer to string for the palindrome check).
  2. Apply a linear scan with a running maximum correctly.
  3. Handle edge cases: empty array, array with no palindromes, single-element array.

Recruiters at product companies often follow up with variations. Common extensions include:

  • Count the total number of palindromes in the array (not just the largest).
  • Find the second-largest palindrome.
  • Return all palindromes in sorted order.
  • Handle a mixed array of integers and strings.

Practising the base case until it takes under 5 minutes from a blank editor is the right preparation level for these rounds. For a broader view of how companies structure their on-campus drives, the campus placement evaluation test guide covers the full format. The best books for placement preparation list covers resources that address string and array problems systematically. If you are working through the full quantitative aptitude track, time and work quantitative aptitude questions covers another problem type that appears alongside coding in the same placement rounds.

Once the palindrome checker runs cleanly in under 5 minutes from a blank editor, you have cleared the bar for this problem type in placement drives. The string-manipulation skill behind it (converting numbers to character sequences, reversing, comparing) appears again in LLM tokenisation and prompt-engineering work. TinkerLLM puts real LLM API calls in your hands for ₹299, and building a small text-pattern or string-processing project there is what moves a resume from “knows the palindrome problem” to “has shipped something with it.”

Primary sources

Frequently asked questions

What does 'largest palindrome' mean in this problem?

It means the palindrome with the highest numeric value in the array. In [131, 54545], the answer is 54545, not 131, even though both are palindromes.

Are single-digit numbers always palindromes?

Yes. Every integer from 0 to 9 has only one digit, so it reads identically in both directions by definition. Single digits always qualify.

What should the program return if no palindrome exists in the array?

Return -1. This is the standard convention in placement-test problem statements for this problem type, including the Oracle campus drive version.

Does this approach work for arrays of strings as well as integers?

Yes. For a string array, convert each word to lowercase before comparing with its reverse, to handle words like 'Radar' and 'Madam' correctly.

What is the time complexity of the largest palindrome solution?

O(n x d), where n is the number of array elements and d is the digit count of the largest number. Space complexity is O(d) for the temporary string.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF