Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Problem: How many ordered pairs (x, y) with 1 <= x < y <= 50 have x y a perfect square? Run the code.
Problem: How many ordered pairs (x, y) with 1 <= x < y <= 50 have x * y a perfect square? Run the code.
print(sum(1 for x in range(1, 51) for y in range(x + 1, 51) if int(round((x * y) ** 0.5)) ** 2 == x * y))What is printed?
- A. 44
- B. 94
- C. 88
- D. 21
Correct answer: A. 44
x·y is a square exactly when x and y have the same squarefree part; there are 44 such pairs with x < y. Allowing x = y adds 50 for 94, ordered pairs give 88, and 21 counts only pairs of perfect squares.
More Coding Interview Problems questions
- 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…
- Problem: How many inversions does [9, 7, 5, 3, 1, 2, 4, 6, 8] have? Run the code. python a = [9, 7, 5, 3, 1, 2, 4, 6, 8] print(sum(1 for i…
- Problem: What is the maximum number of intervals from [[1,5],[2,6],[4,8],[7,9]] that contain a common point? Run the sweep code. python iv…