Find the first n numbers that are palindromes in both base 10 and base k

Quick Overview

Return the first n positive integers that read the same forward and backward both in base 10 and in a given base k. It tests base conversion, palindrome checks, and keeping the search fast once the qualifying numbers grow large.

Find the first n numbers that are palindromes in both base 10 and base k

Company: Compass

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Given two integers `n` and `k`, return the first `n` positive integers, in ascending order, that are palindromes both in base 10 and in base `k`. A number is a palindrome in a base if the sequence of its digits in that base reads the same forward and backward. For example, `9` is a palindrome in base 10, and `9` written in base 2 is `1001`, which is also a palindrome, so `9` qualifies when `k = 2`. ### Function Signature ```python def find_palindromes(n: int, k: int) -> list[int]: ``` ### Rules - Each number is written in its standard form in each base, with no leading zeros. - `0` is not a positive integer and is never part of the answer. - Return the numbers in ascending order. ### Constraints - `1 <= n <= 30` - `2 <= k <= 10` ### Examples ```text Input: n = 5, k = 2 Output: [1, 3, 5, 7, 9] ``` In base 2 these are `1`, `11`, `101`, `111` and `1001`. The even numbers 2, 4, 6 and 8 end in `0` in base 2 but start with `1`, so they fail. ```text Input: n = 6, k = 5 Output: [1, 2, 3, 4, 6, 88] ``` The numbers 1 to 4 are single digits in base 5, 6 is `11` and 88 is `323`. The number 5 is `10` in base 5, and the base-10 palindromes 7, 8, 9 and 11 to 77 all fail in base 5. ```text Input: n = 10, k = 2 Output: [1, 3, 5, 7, 9, 33, 99, 313, 585, 717] ``` In base 2, 33 is `100001`, 99 is `1100011`, 313 is `100111001`, 585 is `1001001001` and 717 is `1011001101`. Every other base-10 palindrome below 717 fails in base 2.

Overview: Return the first n positive integers that read the same forward and backward both in base 10 and in a given base k. It tests base conversion, palindrome checks, and keeping the search fast once the qualifying numbers grow large.

Compass logo
Compass
Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Given two integers n and k, return the first n positive integers, in ascending order, that are palindromes both in base 10 and in base k.

A number is a palindrome in a base if the sequence of its digits in that base reads the same forward and backward. For example, 9 is a palindrome in base 10, and 9 written in base 2 is 1001, which is also a palindrome, so 9 qualifies when k = 2.

Function Signature

def find_palindromes(n: int, k: int) -> list[int]:

Rules

  • Each number is written in its standard form in each base, with no leading zeros.
  • 0 is not a positive integer and is never part of the answer.
  • Return the numbers in ascending order.

Constraints

  • 1 <= n <= 30
  • 2 <= k <= 10

Examples

Input: n = 5, k = 2
Output: [1, 3, 5, 7, 9]

In base 2 these are 1, 11, 101, 111 and 1001. The even numbers 2, 4, 6 and 8 end in 0 in base 2 but start with 1, so they fail.

Input: n = 6, k = 5
Output: [1, 2, 3, 4, 6, 88]

The numbers 1 to 4 are single digits in base 5, 6 is 11 and 88 is 323. The number 5 is 10 in base 5, and the base-10 palindromes 7, 8, 9 and 11 to 77 all fail in base 5.

Input: n = 10, k = 2
Output: [1, 3, 5, 7, 9, 33, 99, 313, 585, 717]

In base 2, 33 is 100001, 99 is 1100011, 313 is 100111001, 585 is 1001001001 and 717 is 1011001101. Every other base-10 palindrome below 717 fails in base 2.