File: check-if-an-array-is-consecutive/solution.py

Date: 2026-06-06

Time: 15:37

Purpose

This file solves LeetCode 2229: Check if an Array is Consecutive. It determines whether a given integer array contains every number in the contiguous range [min, min + n - 1] — i.e., the elements form a complete consecutive sequence regardless of order. The file is self-contained: solution class and unit tests in one module.

Key Components

Solution.isConsecutive(nums: List[int]) -> bool

The core method. It checks two conditions that together are necessary and sufficient for consecutiveness:

1. No duplicates: len(set(nums)) == n — if any value repeats, the array can't cover n distinct consecutive integers.

2. Range matches length: max(nums) - min(nums) + 1 == n — the span of values exactly equals the count of elements.

Both conditions together guarantee every integer in [min, max] is present exactly once.

TestIsConsecutive

Eight test cases covering:

Patterns

Dependencies

Imports: typing.List (type annotation), unittest (test framework). No project-internal dependencies.

Imported by: The "Imported By" list in the prompt is misleading — those are unrelated test files across the repo, not actual importers of this module. The file is standalone.

Flow

1. Compute n = len(nums).

2. Build num_set = set(nums) — O(n) time, O(n) space.

3. If the set is smaller than the array, duplicates exist → return False.

4. Check if the value range max - min + 1 equals n. If so, the unique values span exactly n consecutive integers → return True.

The method makes two passes for max/min (could be one, but Python's builtins are C-optimized so this is fine in practice).

Invariants

Error Handling

None. Empty input would raise ValueError from max()/min(). The LeetCode contract guarantees nums is non-empty, so no defensive check is needed.

Topics to Explore

Beliefs