The median sum that overflows
transcript
Two latencies. Each is two billion. The median averages the two. The sum is four billion. Does it fit?
The largest 32 bit int is 2.147 billion. Four billion does not fit. In Java the sum wraps to a negative. A 64 bit sum holds it.
Cast one value to 64 bit first. Add the two tops. Then halve and round down. The median is two billion.
In Java, the sum wraps silently. In C plus plus, signed overflow is undefined. Python and JavaScript are safe here. But write it safely anyway.
Always test two huge values. Each is two billion. The median must be two billion. A wrapped sum would fail.
Latencies are often in microseconds. A thirty minute timeout is 1.8 billion. Two of them add up to 3.6 billion. That is already past the int limit.
Two big tops overflow an int. Use a 64 bit sum. Full breakdown in the lecture.