Coding questions · Sonatus
Compute days until a higher reading
This question evaluates proficiency in array processing and the next-greater-element concept, measuring algorithmic reasoning about sequence traversal, comparisons, and managing time/space trade-offs.
You are given an array readings[] representing sensor values over consecutive days (e.g., temperatures).
For each day i, compute how many days you must wait until you see a strictly higher reading on a future day j > i. If there is no future day with a higher reading, output 0 for that day.
Input
- An integer array
readingsof lengthn.
Output
- An integer array
ansof lengthnwhereans[i]is the number of days until a higher reading occurs, or0if none exists.
Example
- Input:
readings = [30, 38, 30, 36, 35, 40, 28] - Output:
[1, 4, 1, 2, 1, 0, 0]
Constraints (typical)
1 <= n <= 2e5- Readings fit in 32-bit signed integers.
Notes
- “Higher” means strictly greater (
>), not greater-or-equal.
Days Until a Higher Reading
For each index, return how many days until a strictly higher future reading appears. If none appears, return 0 at that index.
Hints
- Keep a monotonic decreasing stack of unresolved indices.