Data Structures & AlgorithmsModerateTCS NQT · DP warm-up
Problem: How many ordered sequences of 1s and 3s sum to 6? Run the code.
Problem: How many ordered sequences of 1s and 3s sum to 6? Run the code.
ways = [1] + [0] * 6
for v in range(1, 7):
for s in (1, 3):
if v >= s:
ways[v] += ways[v - s]
print(ways[6])What is printed?- A. 6
- B. 3
- C. 4
- D. 9
Correct answer: A. 6
Ordered sequences of 1s and 3s summing to 6: 111111, 33 and the 4 arrangements of one 3 with three 1s, so 6. Putting the step loop outside the target loop counts combinations (3), reading ways[5] gives 4, and reading ways[7] gives 9.
More Coding Interview Problems questions
- Problem: What is the maximum XOR of any contiguous subarray of [8, 1, 2, 12]? Run the brute force. python a = [8, 1, 2, 12] best = 0 for i…
- Problem: Sort [1, 1, 2, 2, 2, 3] by frequency ascending, breaking ties by value ascending. What is printed? python from collections import…
- Problem: How many set bits are there in total across the numbers 0 to 7? Run the code. python print(sum(bin(i).count('1') for i in…
- Problem: How many integers in [1, 20] are powers of two? Run the code. python print(sum(1 for x in range(1, 21) if x & (x - 1) == 0)) What…
- Problem: How many integers in [1, 100] are divisible by 7 or by 11? Run the code. python print(sum(1 for x in range(1, 101) if x % 7 == 0…
- Problem: What is 2^100 mod 7? Run the code. python print(pow(2, 100, 7)) What is printed?