Check whether an array contains every integer in an inclusive range

Quick Overview

Determine whether every integer in an inclusive range [low, high] appears at least once in an unsorted integer array that may contain duplicates and out-of-range values. It tests hash set counting, the pigeonhole shortcut, and avoiding a loop over a huge numeric range.

Check whether an array contains every integer in an inclusive range

Company: EXL

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an integer array `nums` and two integers `low` and `high`, return `True` if every integer in the inclusive range `[low, high]` appears at least once in `nums`. Otherwise, return `False`. `nums` is a one-dimensional array of integers (not an array of intervals). Duplicate values and values outside the range do not affect the answer. ### Function Signature ```python def contains_range(nums: list[int], low: int, high: int) -> bool: ``` ### Constraints - `0 <= len(nums) <= 10^5` - `-10^9 <= nums[i], low, high <= 10^9` - `low <= high` ### Examples ```text Input: nums = [1,2,3,4,5], low = 2, high = 4 Output: True ``` The values 2, 3 and 4 all appear in `nums`, so the range `[2, 4]` is fully covered. ```text Input: nums = [1,3,5,7], low = 1, high = 4 Output: False ``` The values 2 and 4 are missing from `nums`, so the range `[1, 4]` is not fully covered. ```text Input: nums = [4,2,2,9,3], low = 2, high = 4 Output: True ``` 2, 3 and 4 each appear at least once. The repeated 2 and the out-of-range 9 change nothing, and the order of the array does not matter.

Overview: Determine whether every integer in an inclusive range [low, high] appears at least once in an unsorted integer array that may contain duplicates and out-of-range values. It tests hash set counting, the pigeonhole shortcut, and avoiding a loop over a huge numeric range.

EXL logo
EXL
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums and two integers low and high, return True if every integer in the inclusive range [low, high] appears at least once in nums. Otherwise, return False.

nums is a one-dimensional array of integers (not an array of intervals). Duplicate values and values outside the range do not affect the answer.

Function Signature

def contains_range(nums: list[int], low: int, high: int) -> bool:

Constraints

  • 0 <= len(nums) <= 10^5
  • -10^9 <= nums[i], low, high <= 10^9
  • low <= high

Examples

Input: nums = [1,2,3,4,5], low = 2, high = 4
Output: True

The values 2, 3 and 4 all appear in nums, so the range [2, 4] is fully covered.

Input: nums = [1,3,5,7], low = 1, high = 4
Output: False

The values 2 and 4 are missing from nums, so the range [1, 4] is not fully covered.

Input: nums = [4,2,2,9,3], low = 2, high = 4
Output: True

2, 3 and 4 each appear at least once. The repeated 2 and the out-of-range 9 change nothing, and the order of the array does not matter.