contents: revamp basic algo content

This commit is contained in:
Yangshun
2022-04-02 14:43:27 +08:00
parent 83d8625ad8
commit ce79ad0d9b
25 changed files with 1093 additions and 327 deletions

View File

@ -1,12 +1,59 @@
---
id: queue
title: Queue
toc_max_heading_level: 2
---
## Sample questions
## Introduction
- Implement a Queue class from scratch with an existing bug, the bug is that it cannot take more than 5 elements.
- Implement a Queue using two stacks. You may only use the standard `push()`, `pop()`, and `peek()` operations traditionally available to stacks. You do not need to implement the stack yourself (i.e. an array can be used to simulate a stack).
A queue is a linear collection of elements that are maintained in a sequence and can be modified by the addition of elements at one end of the sequence (**enqueue** operation) and the removal of elements from the other end (**dequeue** operation). Usually, the end of the sequence at which elements are added is called the back, tail, or rear of the queue, and the end at which elements are removed is called the head or front of the queue. As an abstract data type, queues can be implemented using arrays or singly linked lists.
This behavior is commonly called FIFO (first in, first out). The name "queue" for this type of structure comes from the analogy to people lining up in real life to wait for goods or services.
Breadth-first search is commonly implemented using queues.
## Implementations
| Language | API |
| --- | --- |
| C++ | [`std::queue`](https://docs.microsoft.com/en-us/cpp/standard-library/queue-class) |
| Java | [`java.util.Queue`](https://docs.oracle.com/javase/10/docs/api/java/util/Queue.html).Use [`java.util.ArrayDeque`](https://docs.oracle.com/javase/10/docs/api/java/util/ArrayDeque.html) |
| Python | [`queue`](https://docs.python.org/3/library/queue.html) |
| JavaScript | N/A |
## Time complexity
| Operation | Big-O |
| ------------- | ----- |
| Enqueue/Offer | O(1) |
| Dequeue/Poll | O(1) |
| Front | O(1) |
| Back | O(1) |
| isEmpty | O(1) |
## Learning resources
- Readings
- [To Queue Or Not To Queue](https://medium.com/basecs/to-queue-or-not-to-queue-2653bcde5b04), basecs
- Videos
- [Queues](https://www.coursera.org/lecture/data-structures/queues-EShpq), University of California San Diego
## Corner cases
- Empty queue
- Queue with one item
- Queue with two items
## Things to look out for during interviews
Most languages don't have a built in Queue class which to be used, and candidates often use arrays (JavaScript) or lists (Python) as a queue. However, note that the enqueue operation in such a scenario will be O(n) because it requires shifting of all other elements by one. In such cases, you can flag this to the interviewer and say that you assume that there's a queue data structure to use which has an efficient enqueue operation.
## Recommended questions
- [Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks)
- [Implement Stack using Queues](https://leetcode.com/problems/implement-queue-using-stacks)
- [Design Circular Queue](https://leetcode.com/problems/design-circular-queue)
- [Design Hit Counter (LeetCode Premium)](https://leetcode.com/problems/design-hit-counter)
## Recommended courses