Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA
How many pairs of distinct elements of [1, 4, 6, 7, 10, 13, 15] have a sum that is a perfect square (1, 4, 9, 16, ...)?
- A. 3
- B. 4
- C. 2
- D. 1
Correct answer: A. 3
Enumerate pairs and test the sum against a set of squares; the sum range is small here, so a set lookup is enough. Testing the product instead of the sum counts a different set of pairs.
More Coding Interview Problems questions
- Closed intervals [2,9], [4,7], [6,12], [10,14], [11,13], [15,18]. What is the total length of the set of points covered by at least one…
- With coins of values 1, 3 and 4 (each usable any number of times), in how many ways (order does not matter) can you make the amount 6?
- What is the length of the longest run of equal consecutive values in [4, 4, 1, 2, 2, 2, 5, 1]?
- Problem: Two sorted arrays of sizes m and n are merged in O(m + n) time with two pointers. What is the complexity of finding the median of…
- Array a = [3, 2, 5, 1, 4, 6, 2, 7]. Consider all contiguous subarrays and count those whose sum is a power of two from the set {1, 2, 4…
- Array a = [4, 7, 1, 8, 3, 6, 5, 2]. What is the largest sum of a contiguous subarray that contains exactly two odd numbers?