1
00:00:00,000 --> 00:00:04,440
Sorted order can remove candidates or
connect overlapping records.

2
00:00:04,720 --> 00:00:07,560
Binary search uses it to locate a
boundary.

3
00:00:07,840 --> 00:00:12,320
Our interval example sorts by start
position and then builds a union.

4
00:00:12,560 --> 00:00:16,040
These patterns have different invariants
and different costs.

5
00:00:16,320 --> 00:00:20,880
Defining the boundary or endpoint policy
before the code prevents several common

6
00:00:20,960 --> 00:00:22,000
interview errors.

7
00:00:22,300 --> 00:00:26,060
Before low, every value is smaller than
the target.

8
00:00:26,260 --> 00:00:29,900
Inside the candidate interval, the
insertion boundary still needs to be

9
00:00:30,340 --> 00:00:30,887
found.

10
00:00:30,993 --> 00:00:34,887
At or after high, every value is at least
the target.

11
00:00:34,913 --> 00:00:38,140
The range includes low and excludes high.

12
00:00:38,220 --> 00:00:43,100
When low equals high, the partition
identifies the first qualifying index,

13
00:00:43,180 --> 00:00:45,500
even if the target itself is absent.

14
00:00:45,733 --> 00:00:49,733
The first middle index is two, whose value
is five.

15
00:00:49,813 --> 00:00:53,626
Set high to two because an earlier five
may exist.

16
00:00:53,680 --> 00:00:58,253
The next middle index is one, also five,
so high becomes one.

17
00:00:58,293 --> 00:01:02,573
The final middle index is zero, whose
value two is too small.

18
00:01:02,613 --> 00:01:04,720
Move low past it to one.

19
00:01:04,746 --> 00:01:08,133
The boundaries meet at index one, the
first five.

20
00:01:08,367 --> 00:01:12,847
Initialize high to the length, which is a
valid insertion boundary even though it is

21
00:01:12,927 --> 00:01:14,687
not an element index.

22
00:01:14,807 --> 00:01:17,994
Read the middle only while low is below
high.

23
00:01:18,020 --> 00:01:22,367
The comparison discards the middle when
its value is too small and keeps it as the

24
00:01:22,487 --> 00:01:24,287
upper boundary otherwise.

25
00:01:24,367 --> 00:01:27,887
Each update strictly reduces the candidate
interval.

26
00:01:28,047 --> 00:01:30,527
Return low when the interval becomes
empty.

27
00:01:30,767 --> 00:01:35,407
Each comparison removes about half the
candidate positions, so search time is

28
00:01:35,527 --> 00:01:36,794
logarithmic.

29
00:01:36,820 --> 00:01:41,487
The indices occupy constant auxiliary
space because the function does not create

30
00:01:41,567 --> 00:01:42,740
slices.

31
00:01:42,874 --> 00:01:47,247
Inserting into a Python list still shifts
later references and can cost linear

32
00:01:47,687 --> 00:01:48,260
time.

33
00:01:48,394 --> 00:01:53,327
Python's bisect_left finds the same kind
of boundary; it does not make the

34
00:01:53,447 --> 00:01:55,567
subsequent insertion logarithmic.

35
00:01:55,800 --> 00:01:59,560
Closed intervals include both their start
and their end.

36
00:01:59,720 --> 00:02:04,093
Shared endpoints therefore belong to both
intervals and make them overlap.

37
00:02:04,147 --> 00:02:06,960
A positive gap keeps intervals separate.

38
00:02:07,000 --> 00:02:11,880
Our input contains six to eight, one to
three, three to five, and ten to

39
00:02:11,960 --> 00:02:12,880
twelve.

40
00:02:12,920 --> 00:02:17,400
Sorting by the start coordinate makes the
next possible overlap adjacent to the

41
00:02:17,600 --> 00:02:19,560
latest accumulated interval.

42
00:02:19,800 --> 00:02:22,760
Read one to three first after sorting.

43
00:02:22,840 --> 00:02:26,120
With no earlier interval, append it to the
result.

44
00:02:26,280 --> 00:02:28,200
Read three to five next.

45
00:02:28,320 --> 00:02:33,200
Its start equals the accumulated end, so
these closed intervals overlap.

46
00:02:33,240 --> 00:02:36,387
Replace the end with the larger endpoint
five.

47
00:02:36,413 --> 00:02:40,440
The accumulated interval now covers one
through five without a gap.

48
00:02:40,667 --> 00:02:42,907
Read six to eight next.

49
00:02:42,987 --> 00:02:47,907
Six is greater than the previous end five,
so append a separate interval.

50
00:02:47,947 --> 00:02:52,627
Read ten to twelve and apply the same rule
because ten exceeds eight.

51
00:02:52,667 --> 00:02:57,654
The final union contains one to five, six
to eight, and ten to twelve.

52
00:02:57,680 --> 00:03:02,667
The original input remains unchanged
because sorted created a separate list.

53
00:03:02,900 --> 00:03:06,420
Reject an interval whose start exceeds its
end.

54
00:03:06,540 --> 00:03:11,460
Then sort with the named interval_start
key function and create an empty

55
00:03:11,540 --> 00:03:12,473
result.

56
00:03:12,527 --> 00:03:16,340
The scan appends an interval when a
positive gap separates it from the

57
00:03:16,580 --> 00:03:17,420
previous end.

58
00:03:17,460 --> 00:03:21,660
Otherwise it preserves the earlier start
and takes the larger end.

59
00:03:21,700 --> 00:03:26,020
This maximum prevents a contained interval
from shortening a union that already

60
00:03:26,100 --> 00:03:27,460
extends farther.

61
00:03:27,700 --> 00:03:32,500
After each step, the result equals the
union of the processed prefix.

62
00:03:32,580 --> 00:03:35,300
Its intervals are sorted and disjoint.

63
00:03:35,380 --> 00:03:39,940
The next start cannot overlap an earlier
result interval without also reaching the

64
00:03:40,100 --> 00:03:41,007
last one.

65
00:03:41,033 --> 00:03:44,700
A contained interval changes nothing when
its end is smaller.

66
00:03:44,740 --> 00:03:49,460
These facts justify a single comparison
against the last accumulated interval

67
00:03:49,540 --> 00:03:52,500
instead of a scan of all earlier
intervals.

68
00:03:52,733 --> 00:03:56,733
Sorting n intervals costs big O of n log n
in the worst

69
00:03:56,813 --> 00:03:57,746
case.

70
00:03:57,800 --> 00:03:59,800
The following scan is linear.

71
00:03:59,826 --> 00:04:04,653
The sorted copy and sort workspace require
linear auxiliary storage, and the

72
00:04:04,893 --> 00:04:08,133
returned union can also contain n
intervals.

73
00:04:08,173 --> 00:04:13,053
Recomputing the union of every prefix is
useful for an explanation trace, but the

74
00:04:13,293 --> 00:04:16,493
submitted function sorts once and scans
once.

75
00:04:16,733 --> 00:04:19,613
An empty search returns boundary zero.

76
00:04:19,853 --> 00:04:24,253
Repeated target values test whether
equality moves toward the first qualifying

77
00:04:24,333 --> 00:04:25,213
index.

78
00:04:25,253 --> 00:04:29,573
A contained interval tests whether the
merge preserves the larger end.

79
00:04:29,613 --> 00:04:32,973
Also test a shared endpoint and a positive
gap.

80
00:04:33,053 --> 00:04:37,933
These cases exercise the comparison signs
that encode the contract, rather than only

81
00:04:38,173 --> 00:04:40,333
confirming one ordinary example.
