Stable ordering · Easy
A batch of request timings is sampled in arrival order. The dashboard needs the earliest request with the worst latency.
Return the index of the largest latency, preferring the earliest on ties.
Input
Call
- Implement
solve(input)and return its result. Do not print fromsolve.
Input object
{ latencies:integer[] }
What each field means
latencies— request durations in batch order.
Output
Return value and rules
- Return the zero-based index of the largest latency.
- If the maximum occurs several times, return its earliest index.
- Return
-1for an empty batch.
Constraints
- 0 <= latencies.length <= 100000
- 0 <= latency <= 1000000000
Examples
Example 1
{
"latencies": [
3,
9,
9,
1
]
}Returns
1Example 2
{
"latencies": []
}Returns
-1Required complexity
O(n) time and O(1) auxiliary space