அல்கோரிதம்queue design
Queue design — bounded constraints
TT
Testlaa Team
May 14, 2026•1 min read
Bounded/blocking/lossy — overflow API முடிவு.
Try this in Python
class BoundedQueue:
def __init__(self, cap: int) -> None:
self.cap = cap
self.buf: list[int | None] = [None] * cap
self.head = self.size = 0
def push(self, x: int) -> bool:
if self.size == self.cap:
return False
self.buf[(self.head + self.size) % self.cap] = x
self.size += 1
return True
def pop(self) -> int | None:
if self.size == 0:
return None
x = self.buf[self.head]
self.head = (self.head + 1) % self.cap
self.size -= 1
return x
q = BoundedQueue(2)
print(q.push(1), q.push(2), q.push(3))
print(q.pop(), q.pop(), q.pop())
Key takeaways
- Ring buffer — head+size.
- Contract — bool vs raise.
- Circular queue skeleton.
Tags:
Stacks & queuesPythonமாணவர்கள்
