Find shortest subarray with range ≥ k
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
## Problem
You are given an integer array `nums` of length `n` and an integer `k`.
Define the **range** of a subarray as:
\[
\max(\text{subarray}) - \min(\text{subarray})
\]
Return the **length of the shortest non-empty contiguous subarray** whose range is **at least** `k`.
If no such subarray exists, return `-1`.
## Input
- `nums`: array of integers (can include negatives)
- `k`: non-negative integer
## Output
- An integer: the minimum length of a qualifying subarray, or `-1` if none exists.
## Constraints (typical interview scale)
- `1 <= n <= 2 * 10^5`
- `-10^9 <= nums[i] <= 10^9`
- `0 <= k <= 10^9`
## Examples
- `nums = [1, 3, 2], k = 2` → answer `2` (subarray `[1,3]` has range `2`)
- `nums = [5, 5, 5], k = 1` → answer `-1`
Quick Answer: This question evaluates proficiency with array algorithms and range-query reasoning, testing understanding of max/min behavior over contiguous subarrays and use of appropriate data-structure techniques; it belongs to the Coding & Algorithms domain.
Return the length of the shortest non-empty contiguous subarray whose max minus min is at least k, or -1 if none exists.
Constraints
- Inputs are provided as Python literals matching the function signature.
- Return a deterministic exact-match result.
Examples
Input: ([1,3,2], 2)
Expected Output: 2
Explanation: Prompt example.
Input: ([5,5,5], 1)
Expected Output: -1
Explanation: No qualifying range.
Hints
- Choose a representation that makes the core operation simple.
- Handle empty and boundary inputs before the main algorithm.