PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

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.

Input: ([4,1,7], 3)

Expected Output: 2

Explanation: Length two qualifies.

Input: ([9], 0)

Expected Output: 1

Explanation: k zero.

Hints

  1. Choose a representation that makes the core operation simple.
  2. Handle empty and boundary inputs before the main algorithm.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)