Skip to content

Review Queues #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions queue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,39 @@ In the real world, queues are formed when a first-come, first-served service is

Another variation of queues is the double-ended queue, which allows for dequeuing from both ends, facilitating both FIFO and LIFO (Last In, First Out) data structures.

The following diagram shows the state of a queue of size 5 when numbers 1 to 4 are enqueued and then 4 enqueues happen. The outcome is 4 numbers extracted in the same order they were inserted.

```ASCII
[Figure 1] Enqueue 1,2,3,4 to a queue and then dequeue 4 times
┌───┬───┬───┬───┬───┐
│ │ │ │ │ │
└───┴───┴───┴───┴───┘
┌───┬───┬───┬───┬───┐
│ 1 │ │ │ │ │
└───┴───┴───┴───┴───┘
┌───┬───┬───┬───┬───┐
│ 1 │ 2 │ │ │ │
└───┴───┴───┴───┴───┘
┌───┬───┬───┬───┬───┐
│ 1 │ 2 │ 3 │ │ │
└───┴───┴───┴───┴───┘
┌───┬───┬───┬───┬───┐
│ 1 │ 2 │ 3 │ 4 │ │
└───┴───┴───┴───┴───┘
┌───┬───┬───┬───┬───┐
│ 2 │ 3 │ 4 │ │ │
└───┴───┴───┴───┴───┘
┌───┬───┬───┬───┬───┐
│ 3 │ 4 │ │ │ │
└───┴───┴───┴───┴───┘
┌───┬───┬───┬───┬───┐
│ 4 │ │ │ │ │
└───┴───┴───┴───┴───┘
┌───┬───┬───┬───┬───┐
│ │ │ │ │ │
└───┴───┴───┴───┴───┘
```

## Implementation

Like [stacks](../stack), queues can be implemented using doubly [linked lists](../linkedlist/) or [arrays and slices](../array/). Here is a linked list implementation:
Expand Down
4 changes: 3 additions & 1 deletion queue/generate_binary_numbers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ TestGenerateBinaryNumbers tests solution(s) with the following signature and pro

func GenerateBinaryNumbers(n int) []string

Given a number n (n>=0) count from 0 to n in binary.
Given a positive integer like n count from 0 to n in binary.

For example given 3 return {"0", "1", "10", "11"}.
*/
func TestGenerateBinaryNumbers(t *testing.T) {
tests := []struct {
Expand Down
4 changes: 2 additions & 2 deletions queue/max_of_sub_arrays_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ TestMaxOfKLengthSubArrays tests solution(s) with the following signature and pro

func MaxOfKLengthSubArrays(numbers []int, k int) ([]int, error)

Given a slice of numbers and an integer k, return a slice containing the maximum in each k-sized
Given a slice of integers and an integer k, return a slice containing the maximum in each k-sized
sub-array (sub-slice) of the input.

For example given {1,2,3,4,5} and k=2, return {2,3,4,5} because:
For example given {1,2,3,4,5} and k=2 return {2,3,4,5} because:

* Sub-arrays of the input with length 2 are {{1,2},{2,3},{3,4},{4,5}}
* The maximum in each of the sub-arrays is {2,3,4,5}.
Expand Down
2 changes: 1 addition & 1 deletion queue/queue_using_stacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ TestQueueUsingStacks tests solution(s) with the following signature and problem
(usingStacks *UsingStacks) enqueue(n int)
(usingStacks *UsingStacks) dequeue() int

Implement a queue of integers using stacks.
Implement a queue of integers using two stacks. The queue should support enqueue and dequeue operations.
*/
func TestQueueUsingStacks(t *testing.T) {
tests := []struct {
Expand Down
6 changes: 4 additions & 2 deletions queue/string_permutations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ TestStringPermutations tests solution(s) with the following signature and proble

func StringPermutations(input string) []string

Given a string like "abc", return all possible permutations like "abc,acb,bac,bca,cab,cba" using
a queue.
Given a string return all possible permutations that can be made by rearranging the letters in the string
using a queue.

For example given "abc" return "abc,acb,bac,bca,cab,cba".
*/
func TestStringPermutations(t *testing.T) {
tests := []struct {
Expand Down
19 changes: 12 additions & 7 deletions queue/symmetrical_binary_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ TestIsTreeSymmetrical tests solution(s) with the following signature and problem

func IsTreeSymmetrical(root *tree.BinaryTreeNode) (bool, error)

Given a binary tree the one above "2,4,4,5,6,6,5", true if it is symmetric (mirrored from the root), and false otherwise.
Given a binary tree return true of it is symmetric and false otherwise. A tree is symmetric if you
can draw a vertical line through the root and then the left subtree is the mirror image of the right subtree.

2
/ \
/ \
4 4
/ \ / \
5 6 6 5
Symmetric Not Symmetric
2 2
/ \ / \
/ \ / \
4 4 3 4
/ \ / \ / \ / \
5 6 6 5 5 6 6 5

For example given "2,4,4,5,6,6,5", shown in the symmetric tree above return true.
Given "2,3,4,5,6,6,5", shown in the not symmetric tree above return false.
*/
func TestIsTreeSymmetrical(t *testing.T) {
tests := []struct {
Expand Down