Number Theory Problems for Coding Interviews
120 Number Theory problems from real coding interviews, each with test cases you can run online. By difficulty they split into 32 basic, 58 intermediate and 30 advanced. The companies that ask them most often are Google, Amazon, Microsoft, Bloomberg.
Solve a few problems of each level, name the pattern after each one and revisit the ones that needed a hint a week later. Practise stating the time and space complexity and the edge cases before you code.
Coding problems
- Greatest Common Divisor of an Array (Basic)
- Count Primes (Intermediate)
- Ugly Number (Basic)
- Greatest Common Divisor of Strings (Intermediate)
- Prime Factorization (Intermediate)
- Chinese Remainder Solver (Basic)
- Prime Factorization Queries (Intermediate)
- Count Divisible Numbers in Range (Intermediate)
- Insert GCDs Between Adjacent Nodes (Basic)
- Ugly Number II (Intermediate)
- Super Ugly Number (Intermediate)
- Smallest Divisor Given a Threshold (Intermediate)
- Kth Smallest Prime Fraction (Advanced)
- Divide Two Integers (Intermediate)
- Perfect Number (Basic)
- Prime Arrangements (Basic)
- Delete Nodes at Prime Positions (Basic)
- Count Prime Paths in a Tree (Advanced)
- Graph Connectivity With Threshold (Advanced)
- Tree of Coprimes (Advanced)
- Number of Beautiful Partitions (Advanced)
- Divisor Game (Basic)
- Prime Number of Set Bits (Basic)
- Count Simplified Fractions (Intermediate)
- Three Divisors (Basic)
- Sum of Four Divisors (Intermediate)
- Closest Divisors (Intermediate)
- Smallest Value After Prime Factor Sums (Intermediate)
- Distinct Prime Factors of Product (Intermediate)
- Closest Prime Numbers in Range (Intermediate)
- Sum of GCDs of All Pairs (Advanced)
- Count Subarrays With GCD Equal to K (Intermediate)
- Count Subarrays With LCM Equal to K (Intermediate)
- Smallest Number With Exactly N Divisors (Intermediate)
- Count Squares of Primes in Range (Basic)
- Sum of Largest Prime Factors (Intermediate)
- Count Numbers Coprime to N in a Range (Intermediate)
- Multiplicative Order Modulo N (Intermediate)
- Smallest Discrete Logarithm (Advanced)
- Modular Inverse Queries (Intermediate)
- Totient Values for a List (Basic)
- Highest Power of a Prime Dividing a Product (Basic)
- Count Abundant Numbers up to N (Basic)
- Divisor Count Queries (Intermediate)
- Maximum GCD Over Pairs (Intermediate)
- Sum of Divisor Function Totals (Advanced)
- Count Numbers Divisible by Any Given Prime (Intermediate)
- Count Coprime Pairs in Array (Advanced)
- Product of All Divisors (Advanced)
- Matrix Determinant Modulo Prime (Advanced)
- Matrix Rank Modulo Prime (Advanced)
- Binomial Modulo Small Prime (Advanced)
- Count Sequences With GCD One (Advanced)
- Ordered Pairs With Given LCM (Intermediate)
- Count Numbers With Prime Digit Sum (Advanced)
- Fibonacci GCD Modulo (Advanced)
- Amicable Numbers Sum Below N (Intermediate)
- Twin Prime Pairs Below N (Basic)
- Largest Prime Gap Below N (Basic)
- Goldbach Partition Count (Intermediate)
- Primes in Segment (Advanced)
- N-th Prime Number (Intermediate)
- Emirp Count Below N (Intermediate)
- Circular Primes Count Below N (Intermediate)
- Sophie Germain Primes Below N (Intermediate)
- Semiprimes Up To N (Intermediate)
- Sum of Primes Below N (Basic)
- Prime Counting Function (Basic)
- Smallest Prime Factor Queries (Basic)
- Integers With Exactly K Distinct Prime Factors (Intermediate)
- Totient Summatory Function (Intermediate)
- Euler Totient Preimage Count (Advanced)
- Next Prime After N (Intermediate)
- Batch Primality Test (Intermediate)
- Largest Prime Factor of N (Basic)
- Smallest Primitive Root of Prime (Advanced)
- Sum of Smallest Prime Factors (Intermediate)
- Smallest Solution of Two Congruences (Intermediate)
- Smallest Solution of Congruence System (Advanced)
- Legendre Symbol (Intermediate)
Related
- Basic Number Theory problems
- Intermediate Number Theory problems
- Advanced Number Theory problems
- Practice coding problems online
- All interview guides
Frequently asked questions
How do I get good at number theory problems?
Practise by level, name the pattern after you solve each problem and review the ones you needed hints for. The list is ordered so you can start with the basic ones.
Can I run number theory problems online here?
Yes. Each problem opens in an editor that runs your code against test cases in Java, Python, C++, Go, JavaScript and more.