PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

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.

  • Medium
  • Tesla
  • Coding & Algorithms
  • Software Engineer

Verify permutation in-place and implement fast power

Company: Tesla

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

1) Given an integer list A of length n, determine whether A is a permutation of [0, 1, ..., n-1]. Do it in-place with O( 1) extra space (no auxiliary boolean arrays, hash sets, or sorting). Aim for an idiomatic Python solution; prefer expressions like all/any and comprehensions where appropriate. Provide an O(n)-time approach (e.g., XOR-based or index-placement) that detects out-of-range values and duplicates, and justify correctness and edge cases. 2) Implement a function power(base: float, exp: int) -> float that computes base^exp without using library exponentiation. Achieve O(log |exp|) time via exponentiation by squaring, handle negative exponents, exp = 0, base = 0, and the minimum 32-bit integer exponent. Use constant extra space (iterative solution preferred) and discuss numerical stability/overflow considerations.

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

You are given an integer list A of length n. Determine whether A contains every value in [0, 1, ..., n-1] exactly once. You must do this in O(n) time and O(1) extra space. You may modify the list in place. Do not use sorting, sets, dictionaries, or auxiliary boolean arrays. Return True if A is a permutation of [0, 1, ..., n-1], otherwise return False. The empty list counts as a valid permutation of the empty range.

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

  1. Try placing each value x at index x by swapping until the current index is correct.
  2. 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

Implement a function that computes base raised to exp without using **, pow, or any library exponentiation routine. Your algorithm must run in O(log |exp|) time and use O(1) extra space, preferably iteratively. Handle positive and negative exponents, exp = 0, base = 0, and very small exponents such as -2147483648. For this problem, define 0^0 = 1.0. If base == 0 and exp < 0, raise ZeroDivisionError. Normal floating-point overflow or underflow is acceptable.

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

  1. Write the exponent in binary: each bit tells you whether to multiply the current power into the answer.
  2. For a negative exponent, invert the base first and then work with the absolute value of the exponent.
Last updated: May 8, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Write SQL Data Transformation Queries - Tesla (medium)
  • Implement a Rollback Key-Value Store - Tesla (hard)
  • Compute suffix sums over waypoints - Tesla (hard)
  • Compute time to burn tree - Tesla (medium)
  • Coordinate workers across two exclusive targets - Tesla (hard)