← 返回 tesla 的题目列表Dojo Pythonic Coding Pair: Permutation Check and Pow
类型:qbank
Dojo phone screen with two compact coding tasks: verify whether a list is a permutation of `[0, 1, ..., n-1]` and implement `pow(a: float, b: int)`. The interviewer pushes hard on optimal memory and Pythonic style.
Requirements
Task 1: given a list of length n, determine whether it is a permutation of [0, 1, 2, ..., n - 1].
Initial implementation with a boolean list may be rejected for extra memory.
Follow-up for task 1: solve in place or without extra memory.
Task 2: implement pow(a: float, b: int).
The interviewer may disallow extra memory beyond the recursive call stack.
Code style matters: concise Python, built-ins, comprehensions, any / all, and clean expression are valued.
Notes
For the permutation check, discuss trade-offs among boolean-array marking, sorting in place, cyclic placement, sum / xor checks, and mutation constraints. Sum / xor alone can miss duplicate patterns unless combined carefully with range checks.
If mutation is allowed, cyclic placement can verify nums[i] == i after swapping each in-range value toward its index; if mutation is not allowed and extra memory is disallowed, sorting in place or arithmetic checks have clearer trade-offs.
For pow, fast exponentiation is the expected direction: recursively or iteratively square the base and halve the exponent, with explicit handling for negative exponents.
This round is less about obscure algorithms and more about whether the candidate improves a working answer into an optimal, idiomatic one under pressure.
Preparation
Implement permutation validation three ways: boolean seen array, in-place cyclic placement, and sort-in-place; explain the memory and mutation assumptions for each.
Implement iterative binary exponentiation for positive and negative integer exponents, including n == 0, a == 0, and large negative exponent cases.
Practice rewriting a verbose solution into idiomatic Python using range, all, tuple swaps, and early returns without hiding edge-case checks.