Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4209a06

Browse files
--wip-- [skip ci]
1 parent 65e302b commit 4209a06

File tree

6 files changed

+215
-0
lines changed

6 files changed

+215
-0
lines changed

‎book/D-interview-questions-solutions.asc

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,90 @@ This algorithm only works with DFS.
843843

844844
//
845845

846+
:leveloffset: +1
847+
848+
=== Solutions for Sorting Questions
849+
(((Interview Questions Solutions, sorting)))
850+
851+
:leveloffset: -1
852+
853+
854+
[#sorting-q-merge-intervals]
855+
include::content/part03/sorting-algorithms.asc[tag=sorting-q-merge-intervals]
856+
857+
The first thing we need to understand is all the different possibilities for overlaps:
858+
859+
image::merge-intervals-cases.png[merge intervals cases]
860+
861+
One way to solve this problem, is sorting by start time. That will eliminate half of the cases!
862+
863+
Since A will always start before B, only 3 cases apply:
864+
- No overlap: `[[1, 3], [4, 6]]`.
865+
- Overlap at the end: `[[1, 3], [2, 4]]`.
866+
- Eclipse: `[[1, 9], [3, 7]]`.
867+
868+
*Algorithm*:
869+
870+
* Sort intervals by start time
871+
* If the `curr`ent interval's start time is _equal_ or less than the `last` interval's end time, then we have an overlap.
872+
** Overlaps has two cases: 1) `curr`'s end is larger 2) `last`'s end is larger. For both cases `Math.max` works.
873+
* If there's no overlap, we add the interval to the solution.
874+
875+
*Implementation*:
876+
877+
[source, javascript]
878+
----
879+
include::interview-questions/merge-intervals.js[tags=description;solution]
880+
----
881+
882+
For the first interval, it will be added straight to the solution array. For all others, we will do the comparison.
883+
884+
*Complexity Analysis*:
885+
886+
- Time: `O(n log n)`. Standard libraries has a sorting time of `O(n log n)`, then we visit each interval in `O(n)`.
887+
- Space: `O(n)`. In the worst-case is when there's no overlapping intervals. The size of the solution array would be `n`.
888+
889+
890+
891+
892+
893+
894+
895+
//
896+
897+
[#sorting-q-FILENAME]
898+
include::content/part03/sorting-algorithms.asc[tag=sorting-q-FILENAME]
899+
900+
RESTATE REQUIREMENTS AND DESCRIPTIONS
901+
902+
*Algorithm*:
903+
904+
* STEP 1
905+
* STEP 2
906+
** STEP 2.1
907+
** STEP 2.2
908+
909+
*Implementation*:
910+
911+
[source, javascript]
912+
----
913+
include::interview-questions/FILENAME.js[tags=description;solution]
914+
----
915+
916+
IMPLEMENTATION NOTES
917+
918+
*Complexity Analysis*:
919+
920+
- Time: `O(?)`. WHY?
921+
- Space: `O(?)`. WHY?
922+
923+
924+
925+
926+
927+
928+
929+
//
846930

847931

848932

‎book/content/part04/sorting-algorithms.asc

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,77 @@ We explored many algorithms some of them simple and other more performant. Also,
139139
// | Tim sort | O(n log n) | O(log n) | Yes | No | No | Yes
140140
|===
141141
// end::table[]
142+
143+
144+
145+
146+
147+
148+
149+
150+
151+
==== Practice Questions
152+
(((Interview Questions, sorting)))
153+
154+
155+
156+
157+
158+
// tag::sorting-q-merge-intervals[]
159+
===== TITLE
160+
161+
*so-1*) _Given an array of intervals `[start, end]`, merge all overlapping intervals._
162+
163+
// end::sorting-q-merge-intervals[]
164+
165+
// _Seen in interviews at: X._
166+
167+
*Starter code*:
168+
169+
[source, javascript]
170+
----
171+
include::../../interview-questions/merge-intervals.js[tags=description;placeholder]
172+
----
173+
174+
*Examples*:
175+
176+
[source, javascript]
177+
----
178+
merge([[0, 2], [2, 4]]); // [[0, 4]] (0-2 overlaps with 2-4)
179+
merge([[2, 2], [3, 4]]); // [[2, 2], [3, 4]] (no overlap)
180+
merge([[1, 10], [3, 4]]); // [[1, 10]] (1-10 covers the other)
181+
----
182+
183+
184+
_Solution: <<sorting-q-merge-intervals>>_
185+
186+
187+
188+
189+
190+
191+
// tag::sorting-q-FILENAME[]
192+
===== TITLE
193+
194+
*sorting_INITIALS-NUMBER*) _._
195+
196+
// end::sorting-q-FILENAME[]
197+
198+
// _Seen in interviews at: X._
199+
200+
*Starter code*:
201+
202+
[source, javascript]
203+
----
204+
include::../../interview-questions/FILENAME.js[tags=description;placeholder]
205+
----
206+
207+
*Examples*:
208+
209+
[source, javascript]
210+
----
211+
FN([]); // 3 (EXPLANATION)
212+
----
213+
214+
215+
_Solution: <<sorting-q-FILENAME>>_

‎book/images/merge-intervals-cases.png

28.3 KB
Loading[フレーム]

‎book/images/merge-intervals-cases.png:Zone.Identifier

Whitespace-only changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// const { } = require('../../src/index');
2+
3+
// tag::description[]
4+
function merge(intervals) {
5+
// end::description[]
6+
// tag::placeholder[]
7+
// write your code here...
8+
// end::placeholder[]
9+
// tag::solution[]
10+
const ans = [];
11+
12+
intervals.sort((a, b) => a[0] - b[0]); // sort by start time
13+
14+
for (let i = 0; i < intervals.length; i++) {
15+
const last = ans[ans.length - 1];
16+
const curr = intervals[i];
17+
if (last && last[1] >= curr[0]) { // check for overlaps
18+
last[1] = Math.max(last[1], curr[1]);
19+
} else ans.push(curr);
20+
}
21+
return ans;
22+
// end::solution[]
23+
// tag::description[]
24+
}
25+
// end::description[]
26+
27+
module.exports = { merge };
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const { merge } = require('./merge-intervals');
2+
// const { } = require('../../src/index');
3+
4+
[merge].forEach((fn) => {
5+
describe(`TOPIC: ${fn.name}`, () => {
6+
it('should work with null/empty', () => {
7+
const actual = fn([]);
8+
const expected = [];
9+
expect(actual).toEqual(expected);
10+
});
11+
12+
it('should work with small case', () => {
13+
const actual = fn([[1, 3]]);
14+
const expected = [[1, 3]];
15+
expect(actual).toEqual(expected);
16+
});
17+
18+
it('should work with other case', () => {
19+
const actual = fn([[0, 1], [1, 3], [3, 5], [6, 6]]);
20+
const expected = [[0, 5], [6, 6]];
21+
expect(actual).toEqual(expected);
22+
});
23+
24+
it('should work with other case', () => {
25+
const actual = fn([[10, 99], [20, 50], [9, 11], [98, 100]]);
26+
const expected = [[9, 100]];
27+
expect(actual).toEqual(expected);
28+
});
29+
});
30+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /