Question 61
HardWhat is the output of this code?
def single(a):
ones = twos =
x a:
ones = (ones ^ x) & ~twos
twos = (twos ^ x) & ~ones
ones
(single([, , , ]), single([, , , , , , ]))
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 output of this code?
def single(a):
ones = twos =
x a:
ones = (ones ^ x) & ~twos
twos = (twos ^ x) & ~ones
ones
(single([, , , ]), single([, , , , , , ]))
3 1
0 99
3 99
2 99
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def two_singles(a):
x = 0
for v in a: x ^= v
low = x & -x
p = q = 0
for v in a:
if v & low: p ^= v
else: q ^= v
return sorted([p, q])
print(two_singles([1, 2, 1, 3, 2, 5]))
[5, 6]
[3, 5]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def reverse_bits(n):
r = 0
for _ in range(8):
r = (r << 1) | (n & 1); n >>= 1
return r
print(reverse_bits(0b00010110), reverse_bits(1))
22 1
104 128
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def count_bits(n):
d = [0] * (n + 1)
for i in range(1, n + 1):
d[i] = d[i >> 1] + (i & 1)
return d
print(count_bits(8))
[0, 1, 1, 2, 1, 2, 2, 3, 1]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
Sign up free to see the answer and a step-by-step explanation.
What is the output of this code?
def gray(n):
return [i ^ (i >> 1) for i in range(1 << n)]
g = gray(3)
print(g, all(bin(g[i] ^ g[i + 1]).count("1") == 1 for i in range(len(g) - 1)))
[0, 2, 3, 1, 5, 7, 6, 4] True
[0, 1, 3, 2, 6, 7, 5, 4] False
Sign up free to see the answer and a step-by-step explanation.
What does x & (x − 1) == 0 test for x > 0?
Whether x is odd
Whether x is prime
Whether x is even
Whether x is a power of 2
Sign up free to see the answer and a step-by-step explanation.
What is −x in two's complement equal to?
x >> 1
~x + 1
~x − 1
x ^ 1
Sign up free to see the answer and a step-by-step explanation.
In 32-bit two's complement, what is the range of signed integers?
−2³² to 2³²
−2³¹ to 2³¹ − 1
−2³¹ − 1 to 2³¹
0 to 2³² − 1
Sign up free to see the answer and a step-by-step explanation.
What is (−1) >>> 28 in Java, where >>> is the unsigned right shift on a 32-bit int?
−1
1
15
0
Sign up free to see the answer and a step-by-step explanation.
How do you get the value of the lowest set bit of n?
n | (n − 1)
n >> 1
n ^ n
n & −n
Sign up free to see the answer and a step-by-step explanation.
[3, 6]
[1, 2]
13 128
104 1
[0, 1, 1, 2, 2, 2, 2, 3, 1]
[1, 1, 2, 1, 2, 2, 3, 1, 2]
[0, 1, 3, 2, 6, 7, 5, 4] True
[0, 1, 2, 3, 4, 5, 6, 7] True