Data Structures & AlgorithmsModerateTCS NQT · bit operations
Problem: How many distinct values does x XOR y take over pairs of distinct elements of [1, 2, 3]? Run the code.
Problem: How many distinct values does x XOR y take over pairs of distinct elements of [1, 2, 3]? Run the code.
from itertools import combinations
print(len({x ^ y for x, y in combinations([1, 2, 3], 2)}))What is printed?- A. 3
- B. 2
- C. 1
- D. 6
Correct answer: A. 3
The three pairs give XOR values 3, 2 and 1, all distinct. Counting pairs instead of distinct values gives 3 as well, so the set form is what is being tested here.
More Coding Interview Problems questions
- Problem: What does the following print? It sums the entries at even positions of the array sorted in descending order. python a = [5, 1…
- Problem: What is the maximum sum of non-adjacent elements of [3, 2, 7, 10]? Run the DP. python a = [3, 2, 7, 10] inc = exc = 0 for x in a…
- Problem: How many ways tile a 1 x 4 strip with 1 x 1 squares and 1 x 2 dominoes? Run the code. python f = [1, 1] for in range(3)…
- Problem: How many trailing zero bits does 40 have in binary? Run the code. python print((40 & -40).bitlength() - 1) What is printed?
- Problem: How many pairs (i < j) of [2, 4, 6, 8] have gcd exactly 2? Run the code. python from itertools import combinations from math…
- Problem: Starting at (0,0) in the grid below, how many cells are reachable by 4-directional moves through '.' cells? Run the BFS. python…