Reviewed by Aditya Kumar · Last reviewed 2026-03-24
**Binary Exponentiation:** x^n in O(log n). x^8 = ((x^2)^2)^2. While n: if n odd, result*=x; x*=x; n//=2. def power(x, n): if n < 0: return 1/power(x, -n) res = 1 while n: if n & 1: res *= x x *= x n >>= 1 return res
Binary Exponentiation: x^n in O(log n). x^8 = ((x^2)^2)^2. While n: if n odd, result=x; x=x; n//=2.
def power(x, n):
if n < 0: return 1/power(x, -n)
res = 1
while n:
if n & 1: res *= x
x *= x
n >>= 1
return res
Red Flag: O(n) multiplications. Pro-Move: 'Binary exp—we use pow(x,n,mod) for crypto and batch scoring.'
According to DataEngPrep.tech, this is one of the most frequently asked Python/Coding interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.