Question 51
MediumWhat is the result of 6 & 3, 6 | 3 and 6 ^ 3?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the result of 6 & 3, 6 | 3 and 6 ^ 3?
3, 6, 5
2, 7, 5
0, 9, 9
2, 7, 7
Sign up free to see the answer and a step-by-step explanation.
Why is right-shifting a negative signed integer in C implementation-defined?
It isn't
Some machines fill with the sign bit (arithmetic shift) and others with 0 (logical shift)
It is the same as division by 2 always
It always crashes
Sign up free to see the answer and a step-by-step explanation.
What is the Gray code of the binary number 1011?
1101
0100
1011
1110
Sign up free to see the answer and a step-by-step explanation.
Which bit trick finds whether two integers have opposite signs?
(a | b) == 0
(a ^ b) < 0
(a & b) < 0
a + b < 0
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
n = 5
bits = [0] * (n + 1)
for i in range(1, n + 1):
bits[i] = bits[i >> 1] + (i & 1)
print(bits)
[0, 1, 1, 1, 2, 2]
[1, 1, 2, 2, 3, 3]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
a = [1, 2, 3]
print([[a[i] for i in range(3) if mask >> i & 1] for mask in range(8)])
[[1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]
[[1, 2, 3]]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
nums = [5, 5, 7, 5]
res = 0
for b in range(32):
if sum((x >> b) & 1 for x in nums) % 3:
res |= 1 << b
print(res)
15
3
Sign up free to see the answer and a step-by-step explanation.
What is the result of ~5 in Python (or for a signed int in C)?
−5
2
250
−6
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
print(bin(1 ^ 4).count('1'))
3
1
2
4
Sign up free to see the answer and a step-by-step explanation.
Two numbers appear once and every other number appears twice in an array. How can both be found in O(n) time and O(1) space?
Use a hash map
It needs O(n²) time
XOR everything to get a ^ b, use any set bit of it to split the numbers into two groups, then XOR each group
Sort the array
Sign up free to see the answer and a step-by-step explanation.
[0, 1, 2, 3, 4, 5]
[0, 1, 1, 2, 1, 2]
[[], [1], [2], [3]]
[[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
7
1