Skip to content

Commit 25afb3d

Browse files
authored
Review Queues (#175)
Add diagram to queues. Add examples to queue rehearsal problems
1 parent 9fe24bc commit 25afb3d

6 files changed

+55
-13
lines changed

queue/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,39 @@ In the real world, queues are formed when a first-come, first-served service is
66

77
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.
88

9+
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.
10+
11+
```ASCII
12+
[Figure 1] Enqueue 1,2,3,4 to a queue and then dequeue 4 times
13+
┌───┬───┬───┬───┬───┐
14+
│ │ │ │ │ │
15+
└───┴───┴───┴───┴───┘
16+
┌───┬───┬───┬───┬───┐
17+
│ 1 │ │ │ │ │
18+
└───┴───┴───┴───┴───┘
19+
┌───┬───┬───┬───┬───┐
20+
│ 1 │ 2 │ │ │ │
21+
└───┴───┴───┴───┴───┘
22+
┌───┬───┬───┬───┬───┐
23+
│ 1 │ 2 │ 3 │ │ │
24+
└───┴───┴───┴───┴───┘
25+
┌───┬───┬───┬───┬───┐
26+
│ 1 │ 2 │ 3 │ 4 │ │
27+
└───┴───┴───┴───┴───┘
28+
┌───┬───┬───┬───┬───┐
29+
│ 2 │ 3 │ 4 │ │ │
30+
└───┴───┴───┴───┴───┘
31+
┌───┬───┬───┬───┬───┐
32+
│ 3 │ 4 │ │ │ │
33+
└───┴───┴───┴───┴───┘
34+
┌───┬───┬───┬───┬───┐
35+
│ 4 │ │ │ │ │
36+
└───┴───┴───┴───┴───┘
37+
┌───┬───┬───┬───┬───┐
38+
│ │ │ │ │ │
39+
└───┴───┴───┴───┴───┘
40+
```
41+
942
## Implementation
1043

1144
Like [stacks](../stack), queues can be implemented using doubly [linked lists](../linkedlist/) or [arrays and slices](../array/). Here is a linked list implementation:

queue/generate_binary_numbers_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ TestGenerateBinaryNumbers tests solution(s) with the following signature and pro
77
88
func GenerateBinaryNumbers(n int) []string
99
10-
Given a number n (n>=0) count from 0 to n in binary.
10+
Given a positive integer like n count from 0 to n in binary.
11+
12+
For example given 3 return {"0", "1", "10", "11"}.
1113
*/
1214
func TestGenerateBinaryNumbers(t *testing.T) {
1315
tests := []struct {

queue/max_of_sub_arrays_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ TestMaxOfKLengthSubArrays tests solution(s) with the following signature and pro
1010
1111
func MaxOfKLengthSubArrays(numbers []int, k int) ([]int, error)
1212
13-
Given a slice of numbers and an integer k, return a slice containing the maximum in each k-sized
13+
Given a slice of integers and an integer k, return a slice containing the maximum in each k-sized
1414
sub-array (sub-slice) of the input.
1515
16-
For example given {1,2,3,4,5} and k=2, return {2,3,4,5} because:
16+
For example given {1,2,3,4,5} and k=2 return {2,3,4,5} because:
1717
1818
* Sub-arrays of the input with length 2 are {{1,2},{2,3},{3,4},{4,5}}
1919
* The maximum in each of the sub-arrays is {2,3,4,5}.

queue/queue_using_stacks_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ TestQueueUsingStacks tests solution(s) with the following signature and problem
1212
(usingStacks *UsingStacks) enqueue(n int)
1313
(usingStacks *UsingStacks) dequeue() int
1414
15-
Implement a queue of integers using stacks.
15+
Implement a queue of integers using two stacks. The queue should support enqueue and dequeue operations.
1616
*/
1717
func TestQueueUsingStacks(t *testing.T) {
1818
tests := []struct {

queue/string_permutations_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ TestStringPermutations tests solution(s) with the following signature and proble
1010
1111
func StringPermutations(input string) []string
1212
13-
Given a string like "abc", return all possible permutations like "abc,acb,bac,bca,cab,cba" using
14-
a queue.
13+
Given a string return all possible permutations that can be made by rearranging the letters in the string
14+
using a queue.
15+
16+
For example given "abc" return "abc,acb,bac,bca,cab,cba".
1517
*/
1618
func TestStringPermutations(t *testing.T) {
1719
tests := []struct {

queue/symmetrical_binary_tree_test.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@ TestIsTreeSymmetrical tests solution(s) with the following signature and problem
1111
1212
func IsTreeSymmetrical(root *tree.BinaryTreeNode) (bool, error)
1313
14-
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.
14+
Given a binary tree return true of it is symmetric and false otherwise. A tree is symmetric if you
15+
can draw a vertical line through the root and then the left subtree is the mirror image of the right subtree.
1516
16-
2
17-
/ \
18-
/ \
19-
4 4
20-
/ \ / \
21-
5 6 6 5
17+
Symmetric Not Symmetric
18+
2 2
19+
/ \ / \
20+
/ \ / \
21+
4 4 3 4
22+
/ \ / \ / \ / \
23+
5 6 6 5 5 6 6 5
24+
25+
For example given "2,4,4,5,6,6,5", shown in the symmetric tree above return true.
26+
Given "2,3,4,5,6,6,5", shown in the not symmetric tree above return false.
2227
*/
2328
func TestIsTreeSymmetrical(t *testing.T) {
2429
tests := []struct {

0 commit comments

Comments
 (0)