Takefumi Yamamura's blog

This blog is for my memorandum.

Takefumi Yamamura's b!og

This blog is for my memorandum

AnimalShelter

Problem

An animal shelter, which holds only dogs and cats. Create the data structures to implement operations such as enqueue, dequeueAny, dequeueDog and dequeueCat.

How to solve

My approach is to simply use separate queues for dogs and cats. Then we store some sort of timestamp to mark when each animal was enqueued. When we called dequeueAny, we compare the front of the dog and cat queue and return the oldest.

Code

Remove all ads