Data Structures & AlgorithmsHardTCS NQT · counting
Problem: How many ways are there to place 2 non-attacking rooks on a 3 x 3 board (distinct squares, different rows and different columns; order does not matter)? Run the code.
Problem: How many ways are there to place 2 non-attacking rooks on a 3 x 3 board (distinct squares, different rows and different columns; order does not matter)? Run the code.
from itertools import combinations cells = [(r, c) for r in range(3) for c in range(3)] print(sum(1 for a, b in combinations(cells, 2) if a[0] != b[0] and a[1] != b[1]))What is printed?
- A. 36
- B. 18
- C. 9
- D. 6
Correct answer: B. 18
Choose two rows (3 ways), two columns (3 ways) and a matching (2 ways), giving 3 * 3 * 2 = 18. Ignoring the matching step gives 9.
More Coding Interview Problems questions
- Problem: How many monotone lattice paths from (0,0) to (2,2) exist on a 3 x 3 grid if the moves are right, down or diagonal (down-right)?…
- Problem: What is the sum of i (5 - i) for i from 1 to 4? Run the code. python print(sum(i (5 - i) for i in range(1, 5))) What is printed?
- Problem: What is the sum of gcd(i, j) over all pairs 1 <= i < j <= 4? Run the code. python from math import gcd print(sum(gcd(i, j) for i…
- Problem: What is the digit sum of 2^100? Run the code. python print(sum(int(c) for c in str(2 100))) What is printed?
- Problem: How many triples (a, b, c) with 1 <= a <= b <= c <= 10 have a + b + c = 12? Run the code. python print(sum(1 for a in range(1…
- Problem: What is the largest prime factor of 600851475143? Run the trial-division code. python n = 600851475143 f = 2 last = 1 while f f…