Skip to content

Commit 06d75d8

Browse files
Merge pull request #80 from amejiarosario/feat/sorting-questions
chore(book): improves grammar
2 parents 57b6eb9 + b956708 commit 06d75d8

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

book/D-interview-questions-solutions.asc

+14-16
Original file line numberDiff line numberDiff line change
@@ -803,21 +803,21 @@ graph G {
803803

804804
The red connections are critical; if we remove any, some servers won't be reachable.
805805

806-
We can solve this problem in one pass using DFS. But for that, we keep track of the nodes that are part of a loop (strongly connected components). To do that, we use the time of visit (or depth in the recursion) each node.
806+
We can solve this problem in one pass using DFS. But for that, we keep track of the nodes that are part of a loop (strongly connected components). We use the time of visit (or depth in the recursion) each node.
807807

808808
For example C, if we start on `c0`, it belongs to group 0, then we move c1, c2, and c3, increasing the depth counter. Each one will be on its own group since there's no loop.
809809

810810
For example B, we can start at `b0`, and then we move to `b1` and `b2`. However, `b2` circles back to `b0`, which is on group 0. We can update the group of `b1` and `b2` to be 0 since they are all connected in a loop.
811811

812-
For an *undirected graph*, If we found a node on our dfs, that we have previously visited, we found a loop! We can mark all of them with the lowest group number. We know we have a critical path when it's a connection that links two different groups. For example A, they all will belong to group 0, since they are all in a loop. For Example B, we will have `b0`, `b1`, and `b2` on the same group while `b3` will be on a different group.
812+
For an *undirected graph*, If we found a node on our DFS, that we have previously visited, we found a loop! We can mark all of them with the lowest group number. We know we have a critical path when it's a connection that links two different groups. For example A, they all will belong to group 0, since they are all in a loop. For Example B, we will have `b0`, `b1`, and `b2` on the same group while `b3` will be on a different group.
813813

814814
*Algorithm*:
815815

816816
* Build the graph as an adjacency list (map + array)
817817
* Run dfs on any node. E.g. `0`.
818818
** Keep track of the nodes that you have seen using `group` array. But instead of marking them as seen or not. Let's mark it with the `depth`.
819819
** Visit all the adjacent nodes that are NOT the parent.
820-
** If we see a node that we have visited yet, do a dfs on it and increase the depth.
820+
** If we see a node that we have visited yet, do a DFS on it and increase the depth.
821821
** If the adjacent node has a lower grouping number, update the current node with it.
822822
** If the adjacent node has a higher grouping number, then we found a critical path.
823823

@@ -863,18 +863,16 @@ The first thing we need to understand is all the different possibilities for ove
863863
// my own image
864864
image::intervals-overlap-cases-owned.png[merge intervals cases]
865865

866-
One way to solve this problem, is sorting by start time. That will eliminate half of the cases!
867-
868-
Since A will always start before B, only 3 cases apply:
869-
- No overlap: `[[1, 3], [4, 6]]`.
870-
- Overlap at the end: `[[1, 3], [2, 4]]`.
871-
- Eclipse: `[[1, 9], [3, 7]]`.
866+
One way to solve this problem is sorting by start time. That will eliminate half of the cases! A will always start before B. Only 3 cases apply:
867+
- No overlap: E.g.,`[[1, 3], [4, 6]]`.
868+
- Overlap at the end: E.g., `[[1, 3], [2, 4]]`.
869+
- Eclipse: E.g.,`[[1, 9], [3, 7]]`.
872870

873871
*Algorithm*:
874872

875873
* Sort intervals by start time
876874
* If the `curr`ent interval's start time is _equal_ or less than the `last` interval's end time, then we have an overlap.
877-
** Overlaps has two cases: 1) `curr`'s end is larger 2) `last`'s end is larger. For both cases `Math.max` works.
875+
** Overlaps has two cases: 1) `curr`'s end is larger 2) `last`'s end is larger. For both cases, `Math.max` works.
878876
* If there's no overlap, we add the interval to the solution.
879877

880878
*Implementation*:
@@ -884,12 +882,12 @@ Since A will always start before B, only 3 cases apply:
884882
include::interview-questions/merge-intervals.js[tags=description;solution]
885883
----
886884

887-
For the first interval, it will be added straight to the solution array. For all others, we will do the comparison.
885+
For the first interval, it will be added straight to the solution array. For all others, we will make a comparison.
888886

889887
*Complexity Analysis*:
890888

891-
- Time: `O(n log n)`. Standard libraries has a sorting time of `O(n log n)`, then we visit each interval in `O(n)`.
892-
- Space: `O(n)`. In the worst-case is when there's no overlapping intervals. The size of the solution array would be `n`.
889+
- Time: `O(n log n)`. Standard libraries have a sorting time of `O(n log n)`, then we visit each interval in `O(n)`.
890+
- Space: `O(n)`. In the worst-case is when there are no overlapping intervals. The size of the solution array would be `n`.
893891

894892

895893

@@ -902,9 +900,9 @@ For the first interval, it will be added straight to the solution array. For all
902900
[#sorting-q-sort-colors]
903901
include::content/part04/sorting-algorithms.asc[tag=sorting-q-sort-colors]
904902

905-
We are asked to sort an array with 3 possible values. If we use the standard sorting method `Array.sort`, that will be `O(n log n)`. However, we are asked to solve in linear time and constant space complexity.
903+
We are asked to sort an array with 3 possible values. If we use the standard sorting method `Array.sort`, that will be `O(n log n)`. However, there's a requirement to solve it in linear time and constant space complexity.
906904

907-
The concept on quicksort can help here. We can choose 1 as a pivot and move everything less than 1 to the left and everything bigger than 1 to the right.
905+
The concept of quicksort can help here. We can choose `1` as a pivot and move everything less than 1 to the left and everything more significant than 1 to the right.
908906

909907
*Algorithm*:
910908

@@ -922,7 +920,7 @@ The concept on quicksort can help here. We can choose 1 as a pivot and move ever
922920
include::interview-questions/sort-colors.js[tags=description;solution]
923921
----
924922

925-
We are using the destructive assigment to swap the elements. Here's another version a little bit more compact.
923+
We are using the destructive assignment to swap the elements. Here's another version a little bit more compact.
926924

927925
[source, javascript]
928926
----

book/content/part04/sorting-algorithms.asc

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ endif::[]
55

66
=== Sorting Algorithms
77

8-
Sorting is one of the most common solutions when we want to extract some insights about a collection of data.
9-
We can sort to get the maximum or minimum value and many algorithmic problems involves sorting data first.
8+
Sorting is one of the most common solutions when we want to extract some insights about data.
9+
We can sort to get the maximum or minimum value, and many algorithmic problems can benefit from sorting.
1010

1111
.We are going to explore three basic sorting algorithms _O(n^2^)_ which have low overhead:
1212
- <<part04-algorithmic-toolbox#bubble-sort>>
@@ -21,15 +21,15 @@ Before we dive into the most well-known sorting algorithms, let's discuss the so
2121

2222
==== Sorting Properties
2323

24-
Sorting implementations with the same time complexity might manipulate the data differently. We want to understand these differences so we can be aware of the side-effects it will have on data or extra resources they will require. For instance, some solutions will need auxiliary memory to store temporary data while sorting while others can do it in place.
24+
Sorting implementations with the same time complexity might manipulate the data differently. We want to understand these differences to be aware of the side effects it will have on data or extra resources they will require. For instance, some solutions will need auxiliary memory to store temporary data while sorting, while others can do it in place.
2525

26-
Sorting properties are stable, adaptive, online and in-place. Let's go one by one.
26+
Sorting properties are stable, adaptive, online, and in-place. Let's go one by one.
2727

2828
===== Stable
2929
(((Sorting, stable)))
3030
An ((stable sorting)) algorithms keep the relative order of items with the same comparison criteria.
3131

32-
This especially useful when you want to sort on multiple phases.
32+
This incredibly useful when you want to sort on multiple phases.
3333

3434
.Let's say you have the following data:
3535
[source, javascript]
@@ -82,7 +82,7 @@ Both results are sorted by `age`; however, having a stable sorting is better if
8282
===== In-place
8383
(((Sorting, in-place)))
8484
An ((in-place sorting)) algorithm would have a _space complexity_ of O(1). In other words, it does not use any other auxiliary memory because it moves the items in the collection itself.
85-
No requiring extra memory for sorting is especially useful for memory constraint environments like robotics, smart devices, or embedded systems in appliances.
85+
No extra memory for sorting is especially useful for large amounts of data or in memory constraint environments like robotics, smart devices, or embedded systems in appliances.
8686

8787
===== Online
8888
(((Sorting, online)))
@@ -111,7 +111,7 @@ include::quick-sort.asc[]
111111
<<<
112112
==== Summary
113113

114-
We explored many algorithms some of them simple and other more performant. Also, we cover the properties of sorting algorithms such as stable, in-place, online and adaptive.
114+
We explored the most common sorting algorithms, some of which are simple and others more performant. Also, we cover the properties of sorting algorithms such as stable, in-place, online, and adaptive.
115115
(((Tables, Algorithms, Sorting Complexities)))
116116
(((Tables, Algorithms, Sorting Summary)))
117117

@@ -162,7 +162,7 @@ We explored many algorithms some of them simple and other more performant. Also,
162162

163163
// end::sorting-q-merge-intervals[]
164164

165-
// _Seen in interviews at: X._
165+
// _Seen in interviews at: Facebook, Amazon, Bloomberg._
166166

167167
*Starter code*:
168168

0 commit comments

Comments
 (0)