All practice tasks

Coding (Algo)

Report the Median After Every Reading

Heap · Hard

A monitoring stream must display the median after every incoming signed reading. Re-sorting every prefix is too slow for a long sequence. The median changes after each insertion and even-sized prefixes require the arithmetic mean of their two middle values.

Return an array containing the median of every non-empty input prefix in the same order as the readings arrive.

Input

Call

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

Input shape

{ nums: integer[] }

Fields

  • nums lists readings in arrival order. Values may repeat and may be negative.

Output

Return value

  • Return one number per input reading. An odd prefix uses its middle sorted value; an even prefix uses the mean of its two middle values.
  • Fractional results ending in .5 are valid. Empty input returns [].

Constraints

  • 0 <= nums.length <= 100000; values are signed integers between -1000000000 and 1000000000.

Examples

Example 1

{
  "nums": [
    5,
    1,
    9,
    3
  ]
}

Returns

[
  5,
  3,
  5,
  4
]

Example 2

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

Returns

[
  -2,
  -1.5
]

Required complexity

O(n log(n + 1)) time and O(n) auxiliary space

What this task practices

Report the Median After Every Reading 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.