<div dir="ltr">Why do you think len is an inherently O(1) operation? Sure, on array-based things it is, but for an arbitrary collection, the only logical assertion is that it's O(n). (For some containers, like arrays, it might be O(1) as well, but you can't assume that in general)​</div>