Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Sort Primes in Range

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

Your question is Sort Primes in Range. 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

Neurologica's scan-analysis pipeline needs the prime-valued calibration intervals within a numeric range. Given two integers, return every prime number between them, inclusive, in ascending order.

Use an efficient sieve-based approach rather than testing each number independently. The range may contain large values, but its width is bounded.

Formal Specification

Implement primes_in_range(start, end).

  • Input: Two integers, start and end, where 0 <= start <= end.
  • Output: A list of all prime integers p such that start <= p <= end, ordered from smallest to largest.
  • A prime number is an integer greater than 1 with exactly two positive divisors.

Constraints

  • 0 <= start <= end <= 10^9
  • 1 <= end - start + 1 <= 10^6
  • Return primes in ascending order
  • Return an empty list when no prime exists in the range

Function Signature

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