Data Structures & AlgorithmsHardTCS NQT · sets
Problem: How many integers in [1, 100] are perfect squares or perfect cubes? Run the code.
Problem: How many integers in [1, 100] are perfect squares or perfect cubes? Run the code.
sq = {x * x for x in range(1, 11)}
cu = {x ** 3 for x in range(1, 5)}
print(len(sq | cu))What is printed?- A. 14
- B. 10
- C. 12
- D. 16
Correct answer: C. 12
There are 10 squares (1 to 100) and 4 cubes (1, 8, 27, 64). The value 1 and 64 are in both sets, so the union has 10 + 4 - 2 = 12 elements.
More Coding Interview Problems questions
- Problem: What is the sum of the digits of 2^10 = 1024? Run the code. python print(sum(int(c) for c in str(2 10))) What is printed?
- Problem: What is the product of the distinct elements of [2, 2, 3, 4, 4]? Run the code. python a = [2, 2, 3, 4, 4] p = 1 for x in set(a)…
- Problem: What is the minimum absolute difference between two values of the sorted list [1, 5, 6, 9]? Run the code. python a = sorted([1…
- Problem: How many 2-element subsets of {1, 2, 3, 4, 5} have an even sum? Run the code. python from itertools import combinations…
- Problem: Which statement about the sparse table data structure for static range-minimum queries is correct?
- Problem: Which statement about heavy-light decomposition for path queries on a tree is correct?