Stack/Queue Flashcards Preview

Cracking the Coding Interview > Stack/Queue > Flashcards

Flashcards in Stack/Queue Deck (5)
Loading flashcards...
1
Q

What are the 4 operations of queues?

A

enqueue(i)–inserts element i at the tail end of the queue;
dequeue()–removes the element at the head of the queue and return its value.

And two additional operators provided for convenience:

size()–returns current size of the queue;
peek()–peeks at the element at the head of the queue without changing the queue.

2
Q

How does the average runtime for “popping” elements off the top of a stack compare with the average runtime for “dequeuing” elements from a queue?

A

To pop an element off of a stack, you just need to remove that element and the rest of the elements stay where they are. So the runtime is O(1).

However, to dequeue the element at the head of a queue, all the other elements need to move forward one, so the runtime isO(n) , with n being the number of elements before any number is dequeued.

3
Q

What is one of the major downfalls of linear queues?

A

one of the major downfalls of traditional linear queues is that they can be slow. It takes time to shift all the elements along during a dequeuing.

4
Q

What are circular queues?

A

circular queues, instead of all the elements needing to shift toward the head, we simply change the position of the head!

5
Q

What is the runtime of circular queues?

A

In fact, circular queues take the runtime down fromO(n) to O(1).