Data Structures & AlgorithmsHardGoogle · Flipkart interview
Problem: AND of every integer in the range [lo, hi].
Problem: AND of every integer in the range [lo, hi].
def range_and(lo, hi):
shift = 0
while lo != hi:
lo >>= 1; hi >>= 1; shift += 1
return lo << shift
print(range_and(26, 30), range_and(12, 15))What is printed?- A. 24 12
- B. 26 12
- C. 16 8
- D. 24 8
Correct answer: A. 24 12
Only the common binary prefix survives, because every lower bit takes both 0 and 1 somewhere in the range. 26 = 11010 and 30 = 11110 share '11' → 11000 = 24; 12 = 1100 and 15 = 1111 share '11' → 1100 = 12.
More Coding Interview Problems questions
- Problem: Ugly numbers have no prime factors other than 2, 3 and 5. Generate them in order with three pointers. python def ugly(n): u =…
- Problem: Convert a fraction to a decimal string, putting a repeating part in parentheses. python def frac(n, d): q, r = divmod(n, d); s =…
- Generating all permutations of n distinct items with backtracking and copying each complete permutation into the result list costs how…
- A disjoint-set forest uses path compression but NO union by rank/size. What is its amortised cost per operation over m operations on n…
- What is the running time of Dijkstra's algorithm with a Fibonacci heap on a graph with V vertices and E edges?
- For an array of n = 10⁶ integers, report the number of DISTINCT values in every window of k = 10⁵ consecutive elements. Which approach is…