use a deque instead of a list, popleft is O(N) on list, O(1) on deque