Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Check Whether a Number Is Prime

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Check Whether a Number Is Prime. Start with the requirements on the right.

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.

Problem

Given an integer n, write a function that returns True if n is a prime number and False otherwise. A prime number is greater than 1 and has exactly two positive divisors: 1 and itself.

Constraints

  • -10^9 <= n <= 10^9
  • The function should correctly handle negative numbers, 0, and 1
  • Aim for better than O(n) time by limiting divisor checks

Function Signature

def is_prime(n):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output