Skip to content

Commit 3293b70

Browse files
authored
More details for array problems (#168)
* Explain Bubble Sort and Insertion Sort * Add more examples to Array rehearsal problems * More clear wording in README.md
1 parent 18820b8 commit 3293b70

10 files changed

+66
-13
lines changed

Diff for: array/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Array
22

3-
Arrays are a fundamental and essential data structure in computer science. They consist of a fixed-size contiguous memory block and offer O(1) read and write time complexity. As a fundamental element of programming languages, arrays come built-in with their core.
3+
Arrays are a fundamental and essential data structure in computer science. They consist of a fixed-size contiguous memory block and offer O(1) read and write time complexity. In Go Like most other programming languages arrays are a built-in data type.
44

55
To provide a real-world analogy, consider an array of athletes preparing for a sprinting match. Each athlete occupies a specific position within the array, typically denoted as 1, 2,…, n. While it is technically possible for each athlete to be in a different position, the positions generally carry some form of significance, such as alphabetical order or seniority within the sport.
66

@@ -22,7 +22,7 @@ func main() {
2222

2323
Although arrays are fundamental data structures in Go, their constant size can make them inflexible and difficult to use in situations where a variable size is required. To address this issue, Go provides [slices](https://blog.golang.org/slices-intro), an abstraction of arrays that offer more convenient access to sequential data typically stored in arrays. When a slice is passed to a function, only the slice header is passed, but it still references the same underlying array data, hence it is possible for the callee to modify the values of the slice and send them back to the caller.
2424

25-
Slices enable adding values using the `append` function, allowing dynamic resizing. Additionally, selectors of the format [low:high] can be used to select or manipulate data in the slice. By utilizing slices instead of arrays, Go programmers gain a more flexible and powerful tool to manage their data structures.
25+
Slices enable adding values using the `append` function, allowing dynamic resizing. Additionally, selectors of the format [low:high] can be used to select or manipulate data in the slice. By utilizing slices instead of arrays, Go programmers benefit from a more flexible and powerful tool to manage their data structures.
2626

2727
```Go
2828
package main

Diff for: array/add_two_numbers_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ TestAddTwoNumbers tests solution(s) with the following signature and problem des
1010
1111
AddTwoNumbers(num1, num2 []int) []int
1212
13-
Given two numbers as an array like [2,9] and [9,9,9] return the sum of the numbers they
14-
represent like [1,0,2,8], because 29+999=1028.
13+
Given two positive integers represented as a slice like {2,9} and {9,9,9} return their sum like {1,0,2,8}.
14+
because 29+999=1028.
1515
*/
1616
func TestAddTwoNumbers(t *testing.T) {
1717
tests := []struct {

Diff for: array/bubble_sort_test.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,29 @@ TestBubbleSort tests solution(s) with the following signature and problem descri
1010
1111
BubbleSort(input []int)
1212
13-
Given an array of unsorted integers, sort the array using the Bubble Sort algorithm.
13+
Sort a slice of unsorted integers using the Bubble Sort algorithm.
14+
1415
The algorithm should be in-place, meaning it should not create a new array and it should
1516
work by swapping elements in the array until it is sorted.
17+
18+
To use Bubble Sort to increasingly sort a slice:
19+
20+
1- Go through each item of the list.
21+
2- If item at n[i] is larger than item at n[i+1], then swap the two elements and repeat the same process for every remaining element(s).
22+
3- If a swap has happened go back to 1, otherwise the list is sorted.
23+
24+
The name Bubble Sort comes from the observation that smaller values bubble up to the top of the list and larger values sink lower until
25+
the list is sorted.
26+
27+
To sort {2,3,1} using Bubble Sort:
28+
29+
1- Compare 2 and 3. Do not swap 2 and 3 because 2 <3.
30+
2- Swap 3 and 1 because 1 < 3, the list becomes {2,1,3}.
31+
3- Swap 1 and 2 because 1 < 2, the list becomes {1,2,3}.
32+
4- Do not swap 2 and 3 because 2 < 3.
33+
5- Do not swap 1 and 2 because 1 < 2.
34+
6- Do not swap 2 and 3 because 2 < 3.
35+
7- The list is sorted, return {1,2,3}.
1636
*/
1737
func TestBubbleSort(t *testing.T) {
1838
tests := []struct {

Diff for: array/equal_sum_subarrays_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ TestEqualSumSubArrays tests solution(s) with the following signature and problem
1212
1313
Given an list of integers A, return two sub-arrays with equal sums without changing the
1414
order of the elements in the list.
15+
16+
For example if given {1,7,3,5} return {1,7} and {3,5} because 1+7 = 3+5 = 8.
1517
*/
1618
func TestEqualSumSubArrays(t *testing.T) {
1719
tests := []struct {

Diff for: array/find_duplicate_in_array_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ TestFindDuplicate tests solution(s) with the following signature and problem des
77
88
FindDuplicate(list []int) int
99
10-
Given a list of n integers (3, 2, 1, 4, 5, 4,...,n) where each number is positive and smaller
11-
than n find the duplicate integer.
10+
Given an unsorted slice of n positive integers like {3,2,1,4,5,4,...,n} where each number is smaller
11+
than n and there is at most one duplicate, return the duplicate value like 4.
1212
*/
1313
func TestFindDuplicate(t *testing.T) {
1414
tests := []struct {

Diff for: array/insertion_sort_test.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,24 @@ TestInsertionSort tests solution(s) with the following signature and problem des
1111
InsertionSort(input []int)
1212
1313
Given an array of unsorted integers, sort the array using the Insertion Sort algorithm.
14-
The algorithm should be in-place, meaning it should not create a new array. The algorithm
15-
works by dividing the input array into sorted and unsorted sections, and moving items from
16-
the unsorted section into the sorted section, one item at a time.
14+
The algorithm should be in-place, meaning it should not create a new array.
15+
16+
To increasingly sort a slice using Insertion Sort:
17+
18+
1- Create a sorted list and an unsorted list. Put the first element of the list in the sorted and the rest of the elements in the unsorted list.
19+
2- Compare the first element of the unsorted list with each element of the sorted list starting from the end and swapping as needed.
20+
3- If there are remaining elements in the unsorted list keep doing 2.
21+
22+
The name Insertion Sort comes from the observation that each element is picked and inserted at the right place.
23+
To save memory space in an in-place implementation of Insertion Sort we use the left side and right side of the input list
24+
and an imaginary divisor as a virtual way of having two separate lists.
25+
26+
To sort {2,3,1} using Insertion Sort:
27+
28+
1- Put 2 in the sorted list and the remaining elements {3,1} in the unsorted list.
29+
2- Do not swap 3 and 2 since 2<3, add 3 to the sorted list so the sorted list is {2,3} and the unsorted list is {1}.
30+
3- Swap 3 and 1 since 1<3 so the sorted list is {2,1,3} and unsorted list is empty. Continue by comparing 1 and 2 and swapping since 1<2 so the list becomes {1,2,3}.
31+
4- Since there are no elements left in the unsorted list, the list is sorted so return {1,2,3}.
1732
*/
1833
func TestInsertionSort(t *testing.T) {
1934
tests := []struct {

Diff for: array/product_of_all_other_elements_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ TestProductOfAllOtherElements tests solution(s) with the following signature and
1212
1313
Given an array of integers A, construct a new array B such that B[i] = product of all items
1414
in A except A[i] without using division in O(n) time.
15+
16+
For example given {1,2,3,4} it should return {24,12,8,6} because:
17+
* 24=2*3*4.
18+
* 12=1*3*4.
19+
* 8=1*2*4.
20+
* 6=1*2*3.
1521
*/
1622
func TestProductOfAllOtherElements(t *testing.T) {
1723
tests := []struct {

Diff for: array/reverse_inplace_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ TestReverseInPlace tests solution(s) with the following signature and problem de
1010
1111
ReverseInPlace(list []int, start, end int)
1212
13-
Given an array of integers, a start index, and an end index, reverse the integers in the
14-
array in-place without using any extra memory.
13+
Given a slice of integers, a start index, and an end index, reverse the integers in the in-place
14+
without using any extra memory.
15+
16+
For example given {1,2,3,4,5,6} and start of 0 and end of 4, it should return {5,4,3,2,1,6} because:
17+
18+
Reverse of items from index 0 to 4 is {5,4,3,2,1} and the remaining item {6} remain unchanged, so
19+
the the resulting slice is {5,4,3,2,1,6}.
1520
*/
1621
func TestReverseInPlace(t *testing.T) {
1722
tests := []struct {

Diff for: array/rotate_k_steps_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ TestRotateKSteps tests solution(s) with the following signature and problem desc
1111
RotateKSteps(list []int, k int)
1212
1313
Given an list of integers and a number k, rotate the array k times.
14+
15+
For example if given {1,2,3} and 3, it should return {1,2,3} because.
16+
Slice after 1 rotation: {3,1,2}.
17+
Slice after 2 rotations: {2,3,1}.
18+
Slice after 3 rotations: {1,2,3}.
1419
*/
1520
func TestRotateKSteps(t *testing.T) {
1621
tests := []struct {

Diff for: array/zero_sum_triplets_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ TestZeroSumTriplets tests solution(s) with the following signature and problem d
1111
ZeroSumTriplets(list []int) [][]int
1212
1313
Given an array of numbers like {1, 2, -4, 6, 3} returns unique triplets from the numbers
14-
with sum that equals zero like {-4, 1, 3}.
14+
with sum that equals zero like {-4, 1, 3} because -4+1+3=0.
1515
*/
1616
func TestZeroSumTriplets(t *testing.T) {
1717
tests := []struct {

0 commit comments

Comments
 (0)