Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: What is the sum of the lengths of all subarrays of an array of length 6? Run the code.
Problem: What is the sum of the lengths of all subarrays of an array of length 6? Run the code.
n = 6 print(sum(i * (n - i + 1) for i in range(1, n + 1)))What is printed?
- A. 21
- B. 35
- C. 56
- D. 63
Correct answer: C. 56
Σ i·(n − i + 1) = 6 + 10 + 12 + 12 + 10 + 6 = 56. 21 counts the subarrays rather than their lengths, i·(n − i) gives 35, and 2⁶ − 1 = 63 counts subsets.
More Coding Interview Problems questions
- Problem: What is the longest run of consecutive 1 bits in the binary representation of 1000? Run the code. python x = 1000 best = cur = 0…
- Problem: How many ways are there to make 15 from unlimited coins of value 1, 5 and 10 (order does not matter)? Run the DP. python ways =…
- Problem: How many integers in [1, 100000] are multiples of 7 whose last digit is 3? Run the code. python print(sum(1 for x in range(1…
- Problem: How many ordered pairs (x, y) with 1 <= x, y <= 12 satisfy x^2 + y^2 <= 100? Run the code. python print(sum(1 for x in range(1…
- Problem: How many lattice points (x, y) with x >= 0, y >= 0 and x + y <= 10 lie in the triangle? Run the code. python print(sum(1 for x in…
- Problem: What is the sum of the ASCII codes of the characters of 'hello'? Run the code. python print(sum(ord(c) for c in 'hello')) What is…