Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: How many squares (k k with k >= 1) are at most 10^6? Run the code.
Problem: How many squares (k * k with k >= 1) are at most 10^6? Run the code.
print(sum(1 for k in range(1, 10 ** 6) if k * k <= 10 ** 6))What is printed?
- A. 999
- B. 100
- C. 1000
- D. 31
Correct answer: C. 1000
k² ≤ 10⁶ holds for k = 1..1000, and 1000 < 10⁶ − 1, so the loop bound does not cut it. A strict k < 1000 gives 999, the cube root gives 100, and 31 counts squares up to 10³.
More Coding Interview Problems questions
- Problem: What is the maximum value of x (100 - x) over integers 0 <= x <= 100? Run the code. python print(max(x (100 - x) for x in…
- Problem: What is the number of trailing zeros in 100! expressed in base 6 (count of factors of 6)? Run the code. python n = 100 c2 = c3 =…
- Problem: How many ways can you pick 2 different integers from [1, 20] whose sum is divisible by 4? Run the code. python print(sum(1 for i…
- Problem: What is the count of 3-digit numbers whose digits are all distinct and in strictly increasing order? Run the code. python…
- Problem: What is the sum of the first 100 positive integers that are not divisible by 3? Run the code. python print(sum(x for x in…
- Problem: How many distinct values does x mod 12 take over the numbers x in [1, 1000] that are not divisible by 4? Run the code. python…