Data Structures & AlgorithmsHardTCS NQT Digital · bit operations
Problem: What does the following print?
Problem: What does the following print?
x = 5 print(x << 3 | x >> 1, x & 6, x ^ 3)
- A. 40 4 6
- B. 42 4 6
- C. 42 0 6
- D. 40 2 6
Correct answer: B. 42 4 6
5 << 3 = 40 and 5 >> 1 = 2, and 40 | 2 = 42 (the | binds weaker than << so the first term is 40 | 2). 5 & 6 = 4 and 5 ^ 3 = 6. Operator precedence is the key trap.
More Coding Interview Problems questions
- Problem: For the string 'mississippi', what is the number of distinct substrings of length 2 that occur at least twice? Run the code…
- Problem: How many Armstrong numbers (numbers equal to the sum of their digits each raised to the power of the digit count) lie in [1…
- Problem: In how many ordered ways can 10 be written as a sum of parts from {1, 2, 3}, where the order of the parts matters? Run the code…
- Problem: How many subsets (including the empty subset) of [1, 2, 3] have XOR equal to 0? Run the code. python a = [1, 2, 3] print(sum(1…
- Problem: What does the following print? python print(bin(10).count('1'), (10).bitlength())
- Problem: How many pairs (i < j) of [2, 3, 4, 5, 6] have gcd(a[i], a[j]) = 1? Run the code. python from math import gcd a = [2, 3, 4, 5, 6]…