DSA interview question
An online game hands out bonus rewards on "prime days": days when the player's streak count is a prime number. The server checks millions of streak values a day, and some are close to 10^9. Your task: decide whether N is prime, meaning greater than 1 with no divisors other than 1 and itself. Testing every number below N is hopeless at this scale; the √N trick from the divisors problem brings each check down to about 30,000 steps, and skipping even numbers halves that again.
Example: 17 → true, 25 → false (5 × 5), 1 → false.
bool isPrime(long long n); // true if n is prime
Input n | Output | Explanation |
|---|---|---|
| 1 | false | 1 is not prime by definition |
| 2 | true | The only even prime |
| 3 | true | |
| 4 | false | 2 × 2 |
| 17 | true | |
| 25 | false | 5 × 5, found when i reaches √25 |
| 97 | true | |
| 1000000007 | true | A well-known large prime |
| 999999937 | true | The largest prime below 10^9 |
| 0 | false |
(Confirm: 0 ≤ n ≤ 10^9 or larger; numbers below 2 are not prime.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in