Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: How many non-negative integer pairs (x, y) satisfy 2x + 3y = 12? Run the code.
Problem: How many non-negative integer pairs (x, y) satisfy 2x + 3y = 12? Run the code.
print(sum(1 for x in range(0, 7) for y in range(0, 5) if 2 * x + 3 * y == 12))What is printed?
- A. 3
- B. 2
- C. 4
- D. 5
Correct answer: A. 3
Fix y so that 12 − 3y is even and non-negative: y = 0, 2, 4 give x = 6, 3, 0, so 3 pairs. Odd y (1, 3) leave an odd remainder, which is why the answer is not 5.
More Coding Interview Problems questions
- Problem: Which algorithm solves the minimum-cost perfect assignment problem (bipartite, n x n cost matrix) in O(n^3) time?
- Problem: What is the complexity of Dinic's algorithm on a unit-capacity bipartite matching graph with V vertices and E edges?
- Problem: In Bellman-Ford, which statement about early termination is correct?
- Problem: What is the time complexity of Prim's algorithm on a graph stored as an adjacency matrix with V vertices?
- Problem: By the Sprague-Grundy theorem, what is the Grundy value of a sum of independent impartial games?
- Problem: What is the minimum number of comparisons in the worst case to find both the maximum and the minimum of n = 10 distinct elements…