Data Structures & AlgorithmsModerateTCS NQT · CAT
Problem: The bit string 1011001 is read as a binary number. What is its value when each bit is XOR-ed with the previous bit (the first bit is kept unchanged), giving a new string, read back in binary?
- A. 117
- B. 107
- C. 110
- D. 89
Correct answer: A. 117
Bits 1011001 become 1, 1⊕0, 0⊕1, 1⊕1, 1⊕0, 0⊕0, 0⊕1 = 1110101 = 117, which is n ⊕ (n >> 1), the Gray code of 89. XOR with the next bit gives 107, a running (prefix) XOR gives 110, and 89 is just the original value.
More Coding Interview Problems questions
- Problem: How many monotone right/down paths go from (0,0) to (3,3) on a 4 x 4 grid when cell (1,2) is blocked? Run the DP. python g = [[0]…
- Problem: How many twin prime pairs (p, p + 2), both prime, have p + 2 below 10000? Run the sieve. python N = 10000 s = [True] (N + 1) s[0]…
- Problem: How many unordered pairs of primes (p <= q) sum to 100? Run the code. python def prime(n): return n > 1 and all(n % d for d in…
- Problem: How many integer lattice points (x, y) lie on the circle x^2 + y^2 = 25? Run the code. python print(sum(1 for x in range(-5, 6)…
- Problem: How many integers in [1, 10000] have decimal digits summing to 12? Run the code. python print(sum(1 for x in range(1, 10001) if…
- Problem: What is the length of the longest palindromic subsequence of 'character'? Run the DP. python def lps(s): n = len(s) dp = [[0] n…