🟢Basic Level Problems (1-15)
Foundation concepts - Usually asked to Junior developers or as warm-up questions
Given an integer n, determine if it is a prime number.
Constraints: 1 ≤ n ≤ 10^6
Hint: Check divisors only up to √n for optimization.
Time: O(√n) | Space: O(1)
Asked by: Google, Microsoft, Amazon, Apple
Count the number of prime numbers less than a non-negative integer n.
Constraints: 0 ≤ n ≤ 5 × 10^6
Hint: Use Sieve of Eratosthenes for efficient bulk prime generation.
Time: O(n log log n) | Space: O(n)
LeetCode #204
Asked by: Amazon, Microsoft, Adobe, Uber
Find the greatest common divisor of two integers a and b.
Constraints: 1 ≤ a, b ≤ 10^9
Hint: Use Euclidean algorithm: gcd(a,b) = gcd(b, a%b).
Time: O(log min(a,b)) | Space: O(1)
Asked by: Google, Facebook, Apple, Netflix
Find the least common multiple of two positive integers a and b.
Constraints: 1 ≤ a, b ≤ 10^6
Hint: Use formula: lcm(a,b) = (a * b) / gcd(a,b).
Time: O(log min(a,b)) | Space: O(1)
Asked by: Microsoft, Amazon, Intel, IBM
Given an integer n, return true if it is a power of two. Otherwise, return false.
Constraints: -2^31 ≤ n ≤ 2^31 - 1
Hint: Use bit manipulation: n > 0 && (n & (n-1)) == 0.
Time: O(1) | Space: O(1)
LeetCode #231
Asked by: Google, Apple, Facebook, LinkedIn
Given an integer n, return true if it is a power of three. Otherwise, return false.
Constraints: -2^31 ≤ n ≤ 2^31 - 1
Hint: Use 1162261467 % n == 0 (largest power of 3 in 32-bit).
Time: O(1) | Space: O(1)
LeetCode #326
Asked by: Google, Microsoft, Amazon
A happy number is defined as: starting with any positive integer, replace the number by the sum of the squares of its digits. Repeat until the number equals 1 (happy) or loops endlessly in a cycle that does not include 1 (not happy).
Constraints: 1 ≤ n ≤ 2^31 - 1
Hint: Use a set to detect cycles, or use Floyd's cycle detection.
Time: O(log n) | Space: O(log n)
LeetCode #202
Asked by: Google, Microsoft, Uber, Airbnb
An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5.
Constraints: -2^31 ≤ n ≤ 2^31 - 1
Hint: Keep dividing by 2, 3, 5 and check if result is 1.
Time: O(log n) | Space: O(1)
LeetCode #263
Asked by: Google, Amazon, Microsoft
Given an integer n, return the least number of perfect square numbers that sum to n.
Constraints: 1 ≤ n ≤ 10^4
Hint: Use dynamic programming or BFS approach.
Time: O(n√n) | Space: O(n)
LeetCode #279
Asked by: Google, Facebook, Amazon, Adobe
Given an integer n, return the number of trailing zeroes in n!.
Constraints: 0 ≤ n ≤ 10^4
Hint: Count factors of 5 in n!. Each 10 = 2×5, and there are more 2s than 5s.
Time: O(log n) | Space: O(1)
LeetCode #172
Asked by: Google, Microsoft, Apple, Bloomberg
Convert an integer to a roman numeral.
Constraints: 1 ≤ num ≤ 3999
Hint: Use greedy approach with value-symbol pairs in descending order.
Time: O(1) | Space: O(1)
LeetCode #12
Asked by: Facebook, Amazon, Microsoft, Apple
Convert a roman numeral to an integer.
Constraints: 1 ≤ s.length ≤ 15, s contains only valid roman numerals
Hint: If current symbol < next symbol, subtract current; otherwise add.
Time: O(n) | Space: O(1)
LeetCode #13
Asked by: Facebook, Microsoft, Amazon, Google
Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number.
Constraints: 1 ≤ columnTitle.length ≤ 7, columnTitle consists only of uppercase English letters.
Hint: Treat it as base-26 conversion with A=1, B=2, ..., Z=26.
Time: O(n) | Space: O(1)
LeetCode #171
Asked by: Microsoft, Google, Amazon, Facebook