Verify permutation in-place and implement fast power
Company: Tesla
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates in-place array manipulation and permutation detection alongside efficient exponentiation implementation, measuring competency in space-constrained algorithm design, correctness reasoning for duplicates and out-of-range values, numerical computation with negative and extreme exponents, and awareness of stability and overflow issues. Classified under Coding & Algorithms, it is commonly asked because it tests practical application of algorithmic complexity and resource constraints as well as conceptual understanding of edge-case handling and numerical behavior; the item emphasizes practical implementation skills with significant conceptual reasoning about correctness and robustness, English.
Part 1: Verify Whether an Array Is a Permutation In-Place
Constraints
- 0 <= len(A) <= 200000
- Each element of A is an integer and may be negative or larger than n-1
- Your algorithm should use O(1) extra space
- Your algorithm should run in O(n) time
Examples
Input: ([2, 0, 1, 3],)
Expected Output: True
Explanation: After in-place swaps, the array can become [0, 1, 2, 3], so it is a valid permutation.
Input: ([1, 1, 0],)
Expected Output: False
Explanation: The value 1 appears twice, so one required value is missing.
Input: ([0, 2, 3],)
Expected Output: False
Explanation: For n = 3, the value 3 is out of the allowed range [0, 2].
Input: ([],)
Expected Output: True
Explanation: The empty list is a permutation of the empty range.
Input: ([1],)
Expected Output: False
Explanation: For n = 1, the only valid permutation is [0].
Solution
def solution(A):
n = len(A)
i = 0
while i < n:
x = A[i]
if x < 0 or x >= n:
return False
if x == i:
i += 1
continue
if A[x] == x:
return False
A[i], A[x] = A[x], A[i]
return True
Time complexity: O(n). Space complexity: O(1).
Hints
- Try placing each value x at index x by swapping until the current index is correct.
- If a value is out of range, or if its target index already holds the same value, you can reject immediately.
Part 2: Implement Fast Power Using Exponentiation by Squaring
Constraints
- -1000000.0 <= base <= 1000000.0
- -2147483648 <= exp <= 2147483647
- Do not use library exponentiation
- Target time complexity: O(log |exp|)
- Target extra space: O(1)
Examples
Input: (2.0, 10)
Expected Output: 1024.0
Explanation: 2^10 = 1024.
Input: (2.0, -3)
Expected Output: 0.125
Explanation: 2^-3 = 1 / 2^3 = 1 / 8.
Input: (-2.0, 5)
Expected Output: -32.0
Explanation: An odd exponent preserves the negative sign.
Input: (0.0, 0)
Expected Output: 1.0
Explanation: For this problem, 0^0 is defined to be 1.0.
Input: (1.0, -2147483648)
Expected Output: 1.0
Explanation: This checks the minimum 32-bit exponent edge case; 1 raised to any power is 1.
Solution
def solution(base, exp):
if exp == 0:
return 1.0
if base == 0.0:
if exp < 0:
raise ZeroDivisionError('0.0 cannot be raised to a negative power')
return 0.0
result = 1.0
b = float(base)
e = exp
if e < 0:
b = 1.0 / b
e = -e
while e > 0:
if e & 1:
result *= b
b *= b
e >>= 1
return result
Time complexity: O(log |exp|). Space complexity: O(1).
Hints
- Write the exponent in binary: each bit tells you whether to multiply the current power into the answer.
- For a negative exponent, invert the base first and then work with the absolute value of the exponent.