← Questions

Check an array is a permutation of 0 to n-1

0
TeslaCodingLast asked Sep 2025

Implement is_zero_based_permutation(nums: list[int]) -> bool.

Given an integer array nums of length n, return True if it contains every integer from 0 to n - 1 exactly once, and False otherwise. Values can be anything, including negatives and numbers >= n.

Constraints

  • 0 <= n <= 10^5
  • Use O(1) extra space. You may rearrange nums in place.
  • Aim for O(n) time.

Examples

nums = [3, 0, 2, 1]   ->  True
nums = [2, 2, 0]      ->  False   # 2 appears twice, 1 is missing
nums = [0, 5, 1]      ->  False   # 5 is out of range

Reconstructed from the reported question; details may differ from what interviewers ask.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding