Data Structures & AlgorithmsModerateTCS NQT · bit operations
Problem: How many unordered pairs from {1, 2, 3, 4, 5, 6} have XOR equal to 7? Run the code.
Problem: How many unordered pairs from {1, 2, 3, 4, 5, 6} have XOR equal to 7? Run the code.
from itertools import combinations print(sum(1 for a, b in combinations([1, 2, 3, 4, 5, 6], 2) if a ^ b == 7))What is printed?
- A. 2
- B. 4
- C. 3
- D. 1
Correct answer: C. 3
1 XOR 6 = 7, 2 XOR 5 = 7 and 3 XOR 4 = 7, and no other pair of these values gives 7.
More Coding Interview Problems questions
- Problem: How many palindromic substrings of length at least 1 does 'abcba' contain? Run the code. python s = 'abcba' print(sum(1 for i in…
- Problem: What is the smallest positive integer whose decimal digits multiply to 36? Run the search code. python x = 1 while True: p = 1…
- Problem: How many ways are there to place 2 non-attacking rooks on a 3 x 3 board (distinct squares, different rows and different columns…
- Problem: How many monotone lattice paths from (0,0) to (2,2) exist on a 3 x 3 grid if the moves are right, down or diagonal (down-right)?…
- Problem: What is the sum of i (5 - i) for i from 1 to 4? Run the code. python print(sum(i (5 - i) for i in range(1, 5))) What is printed?
- Problem: What is the sum of gcd(i, j) over all pairs 1 <= i < j <= 4? Run the code. python from math import gcd print(sum(gcd(i, j) for i…