Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Prime Number Function
00:00
5 left

Prime Number Function

EasyPython

Problem

WebMD Medscape content tooling uses numeric revision tokens that may need prime-number validation before processing. Write a function that determines whether a given integer is prime.

A prime number is an integer greater than 1 with exactly two positive divisors: 1 and itself. Return True when n is prime and False otherwise. Your solution should avoid testing every integer up to n when a smaller search is sufficient.

Formal Specification

  • Input: An integer n.
  • Output: A boolean indicating whether n is prime.
  • Do not modify the input.

Constraints

  • 0 <= n <= 10^12
  • Return False for values less than 2
  • The result must be a boolean
  • Target O(sqrt(n)) time complexity
  • Use O(1) additional space

Function Signature

def is_prime(n):
Interviewer

Your question is Prime Number Function. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.