Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Count Primes Below Query Limit

EasyPython00:00
I
Practice interviewer
Your interviewer
In session
I
Interviewer

Welcome to the Python screen.

The question is on your right: Count Primes Below Query Limit. Read through the requirements first.

Run and submit your code as often as you need. You also have five interviewer messages this session - want to talk through your approach, or are you ready to start coding?

You need to log in / sign up to run or submit.

Problem

In a Google Play mobile performance utility, you need a fast way to answer a simple math-based query: given an integer n, return how many prime numbers are strictly less than n.

A prime number is an integer greater than 1 with exactly two positive divisors: 1 and itself.

Formal Specification

  • Input: An integer n
  • Output: An integer representing the number of primes in the range [0, n)

Constraints

  • 0 <= n <= 5 * 10^6
  • Input is a single integer
  • Return the number of primes strictly less than n

Function Signature

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