First N Prime Numbers
Updated
Problem statement
A prime number is an integer greater than 1 whose only positive divisors are 1 and itself.
Given an integer n, return an array of the n smallest prime numbers in increasing order.
Function
firstPrimes(n: int) → int[]Examples
Example 1
n = 15return = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]These are the first 15 primes. On the way to 47, 1 is skipped because a prime must be greater than 1. The even numbers after 2 and the odd numbers 9, 15, 21, 25, 27, 33, 35, 39 and 45 are skipped because each of them has a divisor other than 1 and itself.
Example 2
n = 1return = [2]2 is the smallest prime, and the only even one.
Example 3
n = 7return = [2,3,5,7,11,13,17]The seventh prime is 17. Between 2 and 17, the odd numbers 9 = 3 * 3 and 15 = 3 * 5 are not prime.
Constraints
1 <= n <= 2000
More Bank of America Onsite Interview questions
- Centered Star PyramidONSITE INTERVIEW · Seen Nov 2025
- Employees Without a ManagerONSITE INTERVIEW · Seen Nov 2025
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2025
- N-th Fibonacci NumberONSITE INTERVIEW · Seen Nov 2025
- Reverse Each Word in a SentenceONSITE INTERVIEW · Seen Nov 2025
- Second Highest SalaryONSITE INTERVIEW · Seen Nov 2025
- Three Numbers That Add Up to a TargetONSITE INTERVIEW · Seen Apr 2025
- Duplicate RowsONSITE INTERVIEW · Seen Oct 2023
- Factorial of a NumberONSITE INTERVIEW · Seen Oct 2023
- Palindrome String CheckONSITE INTERVIEW · Seen Sep 2023
Similar questions at other companies
- Confusing Bib NumbersVerily · ONSITE INTERVIEW
- Plus OneDruva · ONSITE INTERVIEW
- Rotate a Flat Image ClockwiseGoogle · ONSITE INTERVIEWGoogle interview questions
- Matrix MultiplicationeBay · ONSITE INTERVIEWeBay interview questions
- Maximum Product of Three IntegersPelatro · ONSITE INTERVIEW
- Climbing StairsAmazon · ONSITE INTERVIEW
- Maximum 69 NumberHSBC · ONSITE INTERVIEWHSBC interview questions
- Roman to IntegerKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Exact-Sixty Dice Game ValueJane Street · ONSITE INTERVIEWJane Street interview questions
- Find the Missing Number from 1 to nHyperVerge · ONSITE INTERVIEW