FastPrepSmallest Prime at Least N
Problems/Smallest Prime at Least N

Smallest Prime at Least N

Microsoft logoMicrosoft● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The round is not named in the review. It describes two interviews in a row for a Microsoft software engineering internship. The review gives a one-line prime-number question with no rules, next to a task to build the game tic-tac-toe.

That line can be read more than one way. Here you return the least prime that is at least n (n itself when n is prime), for 0 <= n <= 2^31 - 1. Other readings would be the next prime strictly above n, or the lowest prime factor of n.

Our version is about 65% the same as the reported question.

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) → int

Examples

Example 1

n = 14return = 17

14 = 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 = 13

13 is already prime, and the answer may equal n, so the answer is 13.

Example 3

n = 0return = 2

0 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

Interview reports that mention this question

  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern Interviews: 244 Experiences (Part 1 of 6)

Microsoft interview experiences

  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
  • Microsoft SWE Intern, Early 2026: 54 Experiences (Part 2 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int smallestPrime(int n) {
        // Write your code here.
    }
}
Checking account…