All practice tasks

Coding (Algo)

Find the Shortest Signed Threshold Range

Prefix sum · Hard

A telemetry series records both positive and negative adjustments. An alert should identify the shortest uninterrupted period whose net total reaches a threshold. Negative readings prevent the usual positive-only sliding-window rule, and an empty period must never trigger an alert.

Return the smallest length of a non-empty contiguous segment whose signed sum is at least target or zero if none qualifies.

Input

Call

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

Input shape

{ nums: integer[], target: integer }

Fields

  • nums contains signed integer adjustments in their original order; target is a signed threshold.
  • A segment is a non-empty consecutive range of indexes. Negative values cannot be discarded or replaced by absolute values.

Output

Return value

  • Return an integer length. Equality qualifies; a missing result is always zero, never null.
  • Even if target is zero or negative, only non-empty segments count. Empty input returns zero.

Constraints

  • 0 <= nums.length <= 100000; -1000000000 <= nums[i] <= 1000000000; -100000000000000 <= target <= 100000000000000.
  • All prefix sums and results fit a safe JSON integer. Do not mutate input.

Examples

Example 1

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

Returns

3

Example 2

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

Returns

0

Required complexity

O(n) time and O(n) auxiliary space

What this task practices

Find the Shortest Signed Threshold 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 hard 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.