Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many ways are there to place 2 non-attacking rooks on a 4 x 4 board? Run the code.
Problem: How many ways are there to place 2 non-attacking rooks on a 4 x 4 board? Run the code.
from itertools import combinations cells = [(r, c) for r in range(4) for c in range(4)] print(sum(1 for a, b in combinations(cells, 2) if a[0] != b[0] and a[1] != b[1]))What is printed?
- A. 144
- B. 120
- C. 72
- D. 36
Correct answer: C. 72
Choose 2 rows and 2 columns and match them in 2 ways: 6 · 6 · 2 = 72. Ordered placements give 144, all pairs of cells number 120, and forgetting the factor 2 gives 36.
More Coding Interview Problems questions
- Problem: How many integers in [1, 1000] have exactly three distinct decimal digits? Run the code. python print(sum(1 for x in range(1…
- Problem: How many primes p <= 200 have p + 6 also prime? Run the code. python def isp(n): return n > 1 and all(n % d for d in range(2…
- Problem: How many integers in [1, 2000] have an odd number of positive divisors? Run the code. python print(sum(1 for x in range(1, 2001)…
- Problem: How many 3-element subsets of {1, ..., 10} have sum 15? Run the code. python from itertools import combinations print(sum(1 for c…
- Problem: What is the largest k such that 2^k divides 12!? Run the code. python n = 12 c = 0 p = 2 while p <= n: c += n // p p = 2 print(c)…
- Problem: What is the length of the longest common prefix of 'interview', 'internet' and 'interval'? Run the code. python import os…