FastPrepCount Primes Up to and Including N
Problems/Count Primes Up to and Including N

Count Primes Up to and Including N

Capital One logoCapital One● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Two Capital One software engineer reports from 2021 list counting primes as the algorithm question on the final interview day. One says that N itself is part of the range.

Neither report gives limits. Here N can be up to 2 * 10^6, which is our choice. Note that N itself counts when it is prime.

Our version is about 95% the same as the reported questions.

Count the prime numbers p with 0 <= p <= n. A prime has exactly two positive divisors, 1 and the number itself, so 0 and 1 are not prime.

n itself is counted when it is prime.

Function

countPrimes(n: int) → int

Examples

Example 1

n = 45return = 14

Fourteen numbers from 2 to 45 pass the test: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41 and 43. 45 = 5 × 9 itself does not.

Example 2

n = 13return = 6

The primes up to 13 are 2, 3, 5, 7, 11, 13. 13 itself is prime and counts, so the answer is 6.

Example 3

n = 4return = 2

0 and 1 are not prime and 4 = 2 * 2 is not prime either. Only 2 and 3 count, which makes 2.

Constraints

  • 0 <= n <= 2 * 10^6

More Capital One Onsite Interview questions

  • Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
  • Sort ColorsONSITE INTERVIEW · Seen Oct 2026
  • Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
  • Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
  • Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
  • Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
  • Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
  • LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
  • Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
  • Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025

Similar questions at other companies

  • Integers With Multiple Sum of Two CubesCharta Health · PHONE SCREEN · ONSITE INTERVIEWCharta Health interview questions
  • Dice-Walk Winning ProbabilityGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEWHarvey interview questions
  • Integer BreakAmazon · ONSITE INTERVIEW
  • Knight Probability in ChessboardGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Path Between Two Nodes of a Fibonacci TreeDatabricks · ONSITE INTERVIEWDatabricks interview questions
  • Count Staircases with Strictly Increasing StepseBay · ONSITE INTERVIEWeBay interview questions
  • Babysitter Quote with Hourly RatesUpstart · ONSITE INTERVIEWUpstart interview questions
  • Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
  • Maximize the Even-Odd Index Product GapInfosys · ONSITE INTERVIEWInfosys interview questions

Capital One interview experiences

  • Capital One Coding OA Questions: 3 Experiences
  • Capital One Experienced Engineer Power Day and OA: 6 Experiences
  • Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
  • Capital One TDP Agentic Round (AI Coding): 7 Experiences
  • Capital One SWE Power Day, Level Not Reported: 6 Experiences
  • Capital One TIP Intern OA and Power Day: 13 Experiences
All Capital One interview questionsSee Capital One hiring insights
Saved
class Solution {
    public int countPrimes(int n) {
        // Write your code here.
    }
}
Checking account…