Data Structures & AlgorithmsModerateTCS NQT · number theory
Problem: What is the greatest common divisor of 12, 18, 30 and 42? Run the code.
Problem: What is the greatest common divisor of 12, 18, 30 and 42? Run the code.
from math import gcd from functools import reduce print(reduce(gcd, [12, 18, 30, 42]))What is printed?
- A. 6
- B. 2
- C. 3
- D. 12
Correct answer: A. 6
Each value is divisible by 6 and no larger number divides all four, so the gcd is 6.
More Coding Interview Problems questions
- Problem: What is the worst-case time complexity of binary search for the first occurrence of a value in a sorted array that contains…
- Problem: Which algorithm constructs an Euler circuit of a connected graph whose vertices all have even degree in linear time?
- Problem: What is the time complexity of computing the Z-array of a string of length n?
- Problem: Which deterministic algorithm finds the global minimum cut of an undirected weighted graph with V vertices in O(V^3) time?
- Count the non-empty subarrays of a = [3, -1, 4, -1, 5, -9, 2, 6] whose sum is divisible by 5, using prefix sums taken modulo 5. python…
- Items (weight, value) are (5,10), (4,7), (6,12), (3,4) and the knapsack capacity is 10. Each item may be taken at most once. Run the exact…