Find the kth nearest prime to n, breaking distance ties toward the smaller prime

Quick Overview

Given integers n and k, return the kth prime closest to n by absolute difference, where n counts if it is prime and ties go to the smaller prime. It tests generating primes efficiently and visiting candidates in an order that makes the tie rule hold without sorting everything.

Find the kth nearest prime to n, breaking distance ties toward the smaller prime

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an integer `n` and an integer `k`, return the k-th nearest prime number to `n`, measured by absolute difference. Order all prime numbers by `|p - n|` and return the k-th one (1-indexed). If two primes have the same absolute difference, the smaller prime comes first. ### Function Signature ```python def kth_nearest_prime(n: int, k: int) -> int: ... ``` ### Rules - Primes are ordered by the pair `(|p - n|, p)`, so at equal distance the smaller prime comes first. - If `n` is itself prime, it is the nearest prime (distance 0) and counts as the first one. - `k` is 1-indexed: `k = 1` asks for the nearest prime. ### Constraints - `1 <= n <= 10^5` - `1 <= k <= 100` ### Examples ```text Input: n = 10, k = 1 Output: 11 ``` Ordered by `(|p - 10|, p)`, the primes start 11 (distance 1), 7 (distance 3), 13 (distance 3), 5 (distance 5), 3 (distance 7), 17 (distance 7). The first is 11. ```text Input: n = 10, k = 3 Output: 13 ``` 7 and 13 are both at distance 3, and the smaller prime 7 comes first, so the order is 11, 7, 13 and the third nearest is 13. ```text Input: n = 13, k = 2 Output: 11 ``` 13 is prime, so it comes first at distance 0. The next closest primes are 11 (distance 2) and 17 (distance 4), so the second nearest is 11.

Overview: Given integers n and k, return the kth prime closest to n by absolute difference, where n counts if it is prime and ties go to the smaller prime. It tests generating primes efficiently and visiting candidates in an order that makes the tie rule hold without sorting everything.

PracHub logo
General
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an integer n and an integer k, return the k-th nearest prime number to n, measured by absolute difference.

Order all prime numbers by |p - n| and return the k-th one (1-indexed). If two primes have the same absolute difference, the smaller prime comes first.

Function Signature

def kth_nearest_prime(n: int, k: int) -> int:
    ...

Rules

  • Primes are ordered by the pair (|p - n|, p) , so at equal distance the smaller prime comes first.
  • If n is itself prime, it is the nearest prime (distance 0) and counts as the first one.
  • k is 1-indexed: k = 1 asks for the nearest prime.

Constraints

  • 1 <= n <= 10^5
  • 1 <= k <= 100

Examples

Input: n = 10, k = 1
Output: 11

Ordered by (|p - 10|, p), the primes start 11 (distance 1), 7 (distance 3), 13 (distance 3), 5 (distance 5), 3 (distance 7), 17 (distance 7). The first is 11.

Input: n = 10, k = 3
Output: 13

7 and 13 are both at distance 3, and the smaller prime 7 comes first, so the order is 11, 7, 13 and the third nearest is 13.

Input: n = 13, k = 2
Output: 11

13 is prime, so it comes first at distance 0. The next closest primes are 11 (distance 2) and 17 (distance 4), so the second nearest is 11.