Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Prime Numbers and String-to-Int

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

Your question is Prime Numbers and String-to-Int. 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

A Cornerstone OnDemand service receives a maximum employee ID and a configuration value as text. Implement one function that generates every prime number up to the maximum ID and converts the configuration string to a 32-bit signed integer without using library conversion or prime-generation functions.

Formal Specification

Implement find_primes_and_parse(n, s):

  1. Return all prime numbers in the inclusive range [2, n] in ascending order using the Sieve of Eratosthenes.
  2. Parse s manually. Ignore leading ASCII spaces, accept one optional + or -, consume consecutive decimal digits, and stop at the first non-digit after digits begin.
  3. Return 0 if no digits are found.
  4. Clamp results outside the signed 32-bit range to -2147483648 or 2147483647.
  5. Do not use int(), float(), str.strip(), str.isdigit(), regular expressions, or library prime utilities.

Return a dictionary with keys primes and value.

Constraints

  • 0 <= n <= 10^6
  • 0 <= len(s) <= 10^5
  • s contains printable ASCII characters and spaces
  • Return all primes in ascending order
  • Parsed results use the signed 32-bit range

Function Signature

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