Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: In a bit-trick loop, n = 0b101101 is processed by repeatedly computing n &= n − 1 until n becomes 0. How many iterations are performed?
- A. 5
- B. 3
- C. 4
- D. 6
Correct answer: C. 4
n &= n − 1 clears the lowest set bit each time, so the loop runs once per set bit. Dividing by 2 instead counts the bit length, which is a different number.
More Coding Interview Problems questions
- Problem: The XOR of the values 1 through 10 is XOR-ed with the XOR of the values 1 through 9. What is the resulting value?
- Problem: The list [5, 8, 1, 9, 3, 7, 2] is split into two halves (the first ⌈n/2⌉ elements and the rest). Each half is sorted, and the two…
- Problem: Compute the sum of the integers from 1 to 1000 that are divisible by 7 or by 11 but not by 77.
- Problem: How many distinct values does the expression (i j) mod 7 take as i and j range independently over 1 ≤ i, j ≤ 7?
- Problem: What does the following print? python x = [[0] 2] 2 x[0][1] = 5 print(x)
- Problem: What does the following print? python print(0.1 + 0.2 == 0.3, round(2.5), round(3.5))