Data Structures & AlgorithmsModerateTCS NQT · bit operations
Problem: How many pairs (i < j) of [1, 2, 3, 4, 5] have a[i] XOR a[j] = 1? Run the code.
Problem: How many pairs (i < j) of [1, 2, 3, 4, 5] have a[i] XOR a[j] = 1? Run the code.
a = [1, 2, 3, 4, 5] print(sum(1 for i in range(len(a)) for j in range(i + 1, len(a)) if a[i] ^ a[j] == 1))What is printed?
- A. 1
- B. 3
- C. 2
- D. 4
Correct answer: C. 2
The pairs (2,3) and (4,5) differ only in the lowest bit, so they XOR to 1.
More Coding Interview Problems questions
- Problem: How many subarrays of [3, 4, 7, 2, -3, 1, 4, 2] have sum exactly 7? Run the prefix-sum hash code. python from collections import…
- Problem: What is the length of the shortest substring of 'aabcbcdbca' that contains all distinct characters of the string? Run the…
- Problem: What is the Levenshtein distance between 'horse' and 'ros'? Run the DP. python a, b = 'horse', 'ros' dp = [[0] (len(b) + 1) for…
- Problem: What is the sum of |ai - aj| over all pairs i < j of the sorted array [1, 4, 6]? Run the code. python a = sorted([1, 4, 6])…
- Problem: Which complexity does the sieve of Eratosthenes have for finding all primes up to n?
- Problem: How many 3-digit numbers are palindromes? python print(sum(1 for x in range(100, 1000) if str(x) == str(x)[::-1])) What is printed?