FastPrepSum of Primes Up to N
Problems/Sum of Primes Up to N

Sum of Primes Up to N

Capital One logoCapital One● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Adding up every prime from 0 to a chosen n is one of the coding questions in a 2022 Capital One report from a software engineer candidate in McLean.

The report does not say whether n itself is included, or how big n gets. Here a prime n counts, and n goes up to 10^6. The result is a 64-bit integer because the total grows past 32 bits. These are our choices.

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

Add up every prime number p with 0 <= p <= n and return the total. A prime has exactly two positive divisors, 1 and the number itself, so 0 and 1 are not prime.

If n is prime, it is included. If there are no primes in the range, return 0.

Function

sumOfPrimes(n: int) → long

Examples

Example 1

n = 12return = 28

Five primes sit in the range 0 to 12: 2, 3, 5, 7 and 11. Adding them gives 28; 12 itself is even and not prime.

Example 2

n = 2return = 2

2 is prime and counts, because the range includes n itself. The answer is 2.

Example 3

n = 1return = 0

There is no prime from 0 to 1, so the sum is 0.

Constraints

  • 0 <= n <= 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 INTERVIEW
  • Dice-Walk Winning ProbabilityGoogle · PHONE SCREEN · ONSITE INTERVIEW
  • Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEW
  • Integer BreakAmazon · ONSITE INTERVIEW
  • Knight Probability in ChessboardGoldman Sachs · ONSITE INTERVIEW
  • Path Between Two Nodes of a Fibonacci TreeDatabricks · ONSITE INTERVIEW
  • Count Staircases with Strictly Increasing StepseBay · ONSITE INTERVIEW
  • Babysitter Quote with Hourly RatesUpstart · ONSITE INTERVIEW
  • Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
  • Maximize the Even-Odd Index Product GapInfosys · ONSITE INTERVIEW
See Capital One hiring insights
Saved
class Solution {
    public long sumOfPrimes(int n) {
        // Write your code here.
    }
}
Checking account…