Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Check If a Number Is Prime

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

Your question is Check If 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

Twitch may use is_prime as a utility when analyzing numeric identifiers associated with Twitch Chat events. Given a non-negative integer n, determine whether it is a prime number.

A prime number is an integer greater than 1 with exactly two positive divisors: 1 and itself. Return True if n is prime and False otherwise.

Formal Specification

Implement is_prime(n):

  • Input: An integer n where 0 <= n <= 10^9.
  • Output: A boolean indicating whether n is prime.
  • Do not treat 0 or 1 as prime numbers.

Your solution should avoid checking every number up to n when a smaller search is sufficient.

Constraints

  • 0 <= n <= 10^9
  • The input is an integer
  • Return a boolean value

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