Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: What is the largest prime below 1000? Run the code.
Problem: What is the largest prime below 1000? Run the code.
def isp(n):
return n > 1 and all(n % d for d in range(2, int(n ** 0.5) + 1))
print(max(p for p in range(2, 1000) if isp(p)))What is printed?- A. 999
- B. 989
- C. 997
- D. 1009
Correct answer: C. 997
997 has no divisor up to 31. 999 = 27 · 37, 989 = 23 · 43 (it slips past a check that stops at 19), and 1009 is the first prime above 1000.
More Coding Interview Problems questions
- Problem: How many paths go from (0,0) to (4,4) on a 5 x 5 grid using right and down moves while avoiding cell (2,2)? Run the DP. python n…
- Problem: How many ordered pairs (x, y) with 1 <= x < y <= 50 have x y a perfect square? Run the code. python print(sum(1 for x in range(1…
- Problem: How many ways are there to write 50 as an ordered sum a^2 + b^2 with a, b >= 0? Run the code. python print(sum(1 for a in…
- Problem: What is the maximum product of 4 consecutive elements of [1, 2, 3, 4, 5, 6]? Run the code. python a = [1, 2, 3, 4, 5, 6]…
- Problem: How many subsequences of 'abcabc' equal the string 'abc'? Run the DP. python s, t = 'abcabc', 'abc' dp = [1] + [0] len(t) for ch…
- Problem: How many strings of length 5 over {a, b, c} have no two adjacent equal characters? Run the code. python from itertools import…