A deque of values breaks on equal readings
transcript
A deque of values has a bug. It hides behind equal values. Watch the window five, five.
The newer five arrives. It removes the older five from the back. Now the oldest five leaves the window. The front equals five, by value.
The code removes the front. But the front is the newer five. It is still in the window. The deque is empty. The answer is wrong.
Values cannot tell two fives apart. Positions are unique. Compare the front position with the leaving one.
Store the position, not the value. Remove the front only if its position leaves. Then equal values are safe.
In code, the deque holds positions. The leaving position is the oldest one. If the front holds it, remove the front. Otherwise leave the deque alone. Two fives now work correctly.
Positions tell you who leaves. Values cannot tell equal readings apart. Full breakdown in the lecture.