All practice tasks

Coding (Algo)

Shortest Target Range

Sliding window · Medium

A batch planner receives positive record counts from consecutive partitions. It needs the shortest uninterrupted set of partitions that provides at least a target number of records. Partitions cannot be split or reordered.

Return the minimum length of a contiguous range whose sum is at least target, or 0 if none qualifies.

Input

Call

  • Implement solve(input) and return the result. The provided adapter handles JSON input and output.

Input shape

A JSON object { "nums": number[], "target": number }.

Fields

  • nums lists strictly positive integer partition sizes.
  • target is a strictly positive integer.

Output

Return value

  • Return a length, not a sum or an index.
  • A sum equal to target qualifies.
  • If a single partition qualifies the answer is 1.
  • Empty input or an insufficient total returns 0.

Constraints

  • 0 <= nums.length <= 100000.
  • 1 <= nums[i] <= 1000000000 and 1 <= target <= 100000000000000.
  • Zero and negative partition sizes are outside this contract; they require a different approach.

Examples

Example 1

{
  "nums": [
    2,
    3,
    1,
    2,
    4,
    3
  ],
  "target": 7
}

Returns

2

The adjacent values 4 and 3 are the shortest qualifying range.

Example 2

{
  "nums": [
    8,
    1
  ],
  "target": 7
}

Returns

1

Exceeding the target is allowed.

Required complexity

O(n) time and O(1) extra space

What this task practices

Shortest Target Range is a coding (algo) interview exercise that trains prompt interpretation, explicit assumptions, a concrete response, and a clear explanation of tradeoffs. The catalog marks it as medium difficulty. It focuses on Technical Reasoning, Testing, Validation. The signed-in workspace provides the tools for the round and evaluates the attempt against task-specific criteria. Reference solutions, hidden checks, evaluator instructions, and candidate work remain private.

Sign in to startThe workspace and evaluation open after sign in.