Find Palindrome With Fixed Length - Problem

Given an integer array queries and a positive integer intLength, return an array answer where answer[i] is either the queries[i]th smallest positive palindrome of length intLength or -1 if no such palindrome exists.

A palindrome is a number that reads the same backwards and forwards. Palindromes cannot have leading zeros.

Input & Output

Example 1 — Basic Case
$ Input: queries = [1,2,3,4,5,90], intLength = 3
Output: [101,111,121,131,141,999]
💡 Note: For length 3: 1st palindrome is 101, 2nd is 111, 3rd is 121, etc. The 90th palindrome is 999.
Example 2 — Out of Range
$ Input: queries = [2,4,6], intLength = 4
Output: [1001,1111,1221]
💡 Note: For length 4: palindromes are 1001, 1111, 1221, 1331, etc.
Example 3 — Single Digit
$ Input: queries = [1,2,3,4,5,6,7,8,9,10], intLength = 1
Output: [1,2,3,4,5,6,7,8,9,-1]
💡 Note: For length 1: only 9 palindromes exist (1-9), so query 10 returns -1

Constraints

  • 1 ≤ queries.length ≤ 5 × 104
  • 1 ≤ queries[i] ≤ 109
  • 1 ≤ intLength ≤ 15

Visualization

Tap to expand
Find Palindrome With Fixed LengthInputqueries = [1,2,3]intLength = 3ProcessFind 1st, 2nd, 3rdpalindromes of length 3Output[101, 111, 121]Length 3 palindromes:1011111211st2nd3rd🎯 Key Insight: Palindromes follow mathematical patterns!
Understanding the Visualization
1
Input
Array of queries and target length
2
Process
Calculate nth palindrome for each query
3
Output
Array of palindromes or -1 if not found
Key Takeaway
🎯 Key Insight: We only need to generate the first half of a palindrome and mirror it to create the complete palindrome
Asked in
Google 15 Meta 12 Microsoft 8
12.5K Views
Medium Frequency
~25 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen