Smallest Prime at Least N
Updated
Problem statement
Given an integer n, return the smallest prime number that is greater than or equal to n. If n is itself prime, return n.
A prime is an integer greater than 1 whose only positive divisors are 1 and itself. The answer always fits in a signed 32-bit integer, because 231 - 1 = 2,147,483,647 is prime.
Function
smallestPrime(n: int) → intExamples
Example 1
n = 14return = 1714 = 2 * 7, 15 = 3 * 5 and 16 = 2 * 8 are not prime. 17 has no divisor other than 1 and itself, so the answer is 17.
Example 2
n = 13return = 1313 is already prime, and the answer may equal n, so the answer is 13.
Example 3
n = 0return = 20 and 1 are not prime. The smallest prime of all is 2.
Constraints
0 <= n <= 2^31 - 1
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Plus OneDruva · ONSITE INTERVIEW
- Rotate a Flat Image ClockwiseGoogle · ONSITE INTERVIEWGoogle interview questions
- Matrix MultiplicationeBay · ONSITE INTERVIEWeBay interview questions
- Maximum Product of Three IntegersPelatro · ONSITE INTERVIEW
- Climbing StairsAmazon · ONSITE INTERVIEW
- Maximum 69 NumberHSBC · ONSITE INTERVIEWHSBC interview questions
- Roman to IntegerKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Exact-Sixty Dice Game ValueJane Street · ONSITE INTERVIEWJane Street interview questions
- Find the Missing Number from 1 to nHyperVerge · ONSITE INTERVIEW
- Alternating Index Square SumAccenture · ONSITE INTERVIEWAccenture interview questions