1
00:00:00,000 --> 00:00:03,440
Big O describes how work grows as an input
grows.

2
00:00:03,840 --> 00:00:06,800
It does not predict seconds on a
particular machine.

3
00:00:07,120 --> 00:00:12,000
Our examples count matches, compare pairs,
and repeatedly halve a candidate

4
00:00:12,080 --> 00:00:12,480
count.

5
00:00:13,120 --> 00:00:17,600
You will see why these operations have
linear, quadratic, and logarithmic costs.

6
00:00:18,240 --> 00:00:22,400
The final explanation must also include
additional memory and the assumptions

7
00:00:22,480 --> 00:00:23,920
behind each operation.

8
00:00:24,233 --> 00:00:26,606
Define the input size first.

9
00:00:26,660 --> 00:00:29,886
For these lists, n is the number of
entries.

10
00:00:30,020 --> 00:00:34,873
Count one equality comparison whenever the
code tests an entry or a pair.

11
00:00:35,033 --> 00:00:38,513
Ignore constant factors when you name the
eventual growth rate.

12
00:00:38,553 --> 00:00:40,580
Big O is an upper bound.

13
00:00:40,606 --> 00:00:45,353
In an interview, give the tightest useful
bound your argument supports, rather than

14
00:00:45,553 --> 00:00:47,913
calling every smaller cost quadratic.

15
00:00:48,167 --> 00:00:52,327
The first function scans every category
code and counts matches with the

16
00:00:52,607 --> 00:00:53,527
target.

17
00:00:53,607 --> 00:00:57,554
It keeps a separate comparison counter so
the work is visible.

18
00:00:57,580 --> 00:01:02,527
The condition increments matches only when
the current value equals the target.

19
00:01:02,567 --> 00:01:07,074
Every entry still receives one comparison,
whether it matches or not.

20
00:01:07,100 --> 00:01:11,367
A result with no matches therefore does
not make this scan cheaper in its growth

21
00:01:11,447 --> 00:01:12,247
rate.

22
00:01:12,467 --> 00:01:16,467
The first four matches, so the match count
becomes one after one

23
00:01:16,547 --> 00:01:17,587
comparison.

24
00:01:17,667 --> 00:01:22,387
The following seven does not match, but
the comparison count still increases.

25
00:01:22,427 --> 00:01:25,987
The second four matches and raises the
match count to two.

26
00:01:26,067 --> 00:01:29,827
At each step, matches describes the
processed prefix.

27
00:01:29,867 --> 00:01:34,307
The next unread value cannot change how
many matches that earlier prefix already

28
00:01:34,827 --> 00:01:35,427
contains.

29
00:01:35,667 --> 00:01:40,467
Nine does not match the target, so the
answer stays at two while the comparison

30
00:01:40,547 --> 00:01:42,200
count becomes four.

31
00:01:42,254 --> 00:01:44,814
The final seven also does not match.

32
00:01:44,840 --> 00:01:48,600
The function returns two matches and five
comparisons.

33
00:01:48,654 --> 00:01:52,867
For a list of n entries, it always makes n
comparisons.

34
00:01:52,947 --> 00:01:57,347
Its time is linear, and its fixed set of
counters requires constant auxiliary

35
00:01:57,427 --> 00:01:58,227
space.

36
00:01:58,467 --> 00:02:01,227
The outer loop fixes a left position.

37
00:02:01,267 --> 00:02:05,667
The inner loop begins one position later,
so it visits each unordered pair

38
00:02:05,747 --> 00:02:09,160
once and never compares a position with
itself.

39
00:02:09,214 --> 00:02:12,107
Add the partner counts across outer
iterations.

40
00:02:12,147 --> 00:02:16,867
They are n minus one, n minus two, and so
on down to zero.

41
00:02:16,947 --> 00:02:20,747
Their sum is n times n minus one, divided
by two.

42
00:02:20,787 --> 00:02:22,947
Its leading growth is quadratic.

43
00:02:23,167 --> 00:02:28,047
At four entries, the linear scan makes
four comparisons, the pair scan makes

44
00:02:28,127 --> 00:02:31,487
six, and repeated halving takes two steps.

45
00:02:31,607 --> 00:02:36,047
At eight entries, those counts are eight,
twenty-eight, and three.

46
00:02:36,127 --> 00:02:40,820
At sixteen entries, they are sixteen, one
hundred twenty, and four.

47
00:02:40,874 --> 00:02:45,007
Doubling the input doubles the linear
count, moves the pair count toward four

48
00:02:45,087 --> 00:02:48,487
times its earlier value, and adds one
halving step.

49
00:02:48,527 --> 00:02:51,327
These counts come from the executed
examples.

50
00:02:51,567 --> 00:02:56,554
Each iteration divides the remaining count
by two with integer division.

51
00:02:56,580 --> 00:03:00,087
The loop stops when at most one candidate
remains.

52
00:03:00,127 --> 00:03:03,487
The step counter records how many
divisions occurred.

53
00:03:03,527 --> 00:03:08,527
To reach one from n by repeated halving
needs a number of steps proportional to

54
00:03:08,647 --> 00:03:09,607
log n.

55
00:03:09,647 --> 00:03:13,887
The logarithm base changes a constant
factor, so it does not change the Big O

56
00:03:13,967 --> 00:03:14,927
class.

57
00:03:15,167 --> 00:03:19,567
A comparison sort commonly needs big O of
n log n work.

58
00:03:19,607 --> 00:03:24,607
An include or exclude search has two to
the n complete decisions, one for each

59
00:03:24,687 --> 00:03:25,620
subset.

60
00:03:25,674 --> 00:03:29,727
A permutation search has n factorial
possible orders for distinct

61
00:03:30,207 --> 00:03:30,727
values.

62
00:03:30,767 --> 00:03:33,540
Count the work at each completed answer
too.

63
00:03:33,674 --> 00:03:37,274
Copying a subset or permutation can add a
factor of n.

64
00:03:37,300 --> 00:03:42,287
A known-index list read, by contrast,
takes constant time under the usual model.

65
00:03:42,600 --> 00:03:46,920
A worst-case cost bounds every valid input
of a given size.

66
00:03:46,960 --> 00:03:51,080
An expected cost depends on a
probabilistic assumption, such as a

67
00:03:51,160 --> 00:03:53,680
suitable distribution of dictionary
hashes.

68
00:03:53,720 --> 00:03:57,400
An amortized cost spreads the total work
across a sequence of

69
00:03:57,480 --> 00:03:58,760
operations.

70
00:03:58,840 --> 00:04:03,720
List append is constant amortized time
even though one resize can copy a linear

71
00:04:03,840 --> 00:04:05,280
number of references.

72
00:04:05,320 --> 00:04:09,400
Expected and amortized describe different
reasons for a bound.

73
00:04:09,633 --> 00:04:14,593
Auxiliary space usually excludes the input
and returned output, but includes

74
00:04:14,673 --> 00:04:17,073
temporary collections and call frames.

75
00:04:17,153 --> 00:04:21,660
Output space matters separately when the
task requests many answers.

76
00:04:21,766 --> 00:04:24,153
Hidden Python copies also count.

77
00:04:24,193 --> 00:04:28,753
A list slice creates new reference slots,
string concatenation creates a new

78
00:04:28,833 --> 00:04:32,193
string, and a recursive call occupies a
frame.

79
00:04:32,273 --> 00:04:36,353
State the convention before you call an
algorithm constant space.

80
00:04:36,600 --> 00:04:41,160
State the input and output contract,
including duplicates and whether you may

81
00:04:41,240 --> 00:04:42,440
change the input.

82
00:04:42,680 --> 00:04:47,440
Explain an invariant that connects each
update to a correct partial result.

83
00:04:47,640 --> 00:04:52,520
Test an ordinary case and a boundary case
before naming the final costs.

84
00:04:52,680 --> 00:04:55,800
Count the repeated operations in your
actual code.

85
00:04:55,880 --> 00:05:00,520
Two nested loops do not automatically
imply quadratic work if their combined

86
00:05:00,600 --> 00:05:03,000
visits are bounded by a single pass.
