PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Coding & Algorithms/Meta

Find shortest subarray with range ≥ k

Last updated: Mar 29, 2026

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.

Related Interview Questions

  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve a Key-Door Corridor Maze - Meta (medium)
  • Solve Array Merge and Parentheses Cleanup - Meta (medium)
  • Solve Two Backtracking Array Problems - Meta (hard)
  • Solve Maze and Suffix Problems - Meta (medium)
Meta logo
Meta
Oct 23, 2025, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
2
0

Problem

You are given an integer array nums of length n and an integer k.

Define the range of a subarray as:

max⁡(subarray)−min⁡(subarray)\max(\text{subarray}) - \min(\text{subarray})max(subarray)−min(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

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Meta•More Software Engineer•Meta Software Engineer•Meta Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ 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
  • Compare Platforms
  • Discord Community

Support

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

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.