Editorial for Design an Ordered Stream


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
class OrderedStream:
    ptr = 0

    def __init__(self, size: int):
        self.data = [None] * size

    def insert(self, idKey: int, value: str):
        idKey -= 1  # 0-indexed
        self.data[idKey] = value
        if idKey > self.ptr:
            return []  # not reaching ptr

        while self.ptr < len(self.data) and self.data[self.ptr]:
            self.ptr += 1  # update self.ptr
        return self.data[idKey:self.ptr]

Comments

There are no comments at the moment.