All practice tasks

Coding (Algo)

Count Matching Ranges

Prefix sum · Medium

A ledger stores signed balance changes in their original order. An analyst wants to know how many uninterrupted ranges add up to a requested adjustment. A range may include refunds and zero-value entries; it cannot skip entries.

Implement solve(input) to return the number of non-empty contiguous ranges whose sum equals target. Count ranges by their start and end positions rather than by their values.

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 is an array of signed integers in ledger order.
  • target is the required total. Both negative and zero targets are valid.

Output

Return value

  • Return one integer count. Do not return the ranges themselves.
  • Different positions count separately even if the values are identical.
  • An empty array returns 0. Empty ranges never count.

Constraints

  • 0 <= nums.length <= 100000.
  • Each value and target is an integer between -1000000000 and 1000000000.
  • All sums and the maximum answer 5000050000 fit exactly in JavaScript Number. Do not truncate the answer to 32 bits.

Examples

Example 1

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

Returns

3

The matching index ranges are [0,0], [0,2] and [2,2].

Example 2

{
  "nums": [
    0,
    0,
    0
  ],
  "target": 0
}

Returns

6

Three length-one ranges, two length-two ranges and one length-three range.

Required complexity

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

What this task practices

Count Matching Ranges 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.