1
00:00:00,000 --> 00:00:04,800
A Python list stores a sequence of object
references at indexed positions.

2
00:00:05,280 --> 00:00:09,600
It is the usual Python representation for
an interview problem about an array.

3
00:00:10,240 --> 00:00:14,480
Our example keeps nonzero values at the
front of an existing list.

4
00:00:15,040 --> 00:00:19,920
Before we solve it, we will follow append,
insertion, removal, and copying.

5
00:00:20,560 --> 00:00:25,040
These operations explain why a solution
can produce the right answer yet have the

6
00:00:25,120 --> 00:00:26,960
wrong time or space claim.

7
00:00:27,267 --> 00:00:29,907
An index identifies a position.

8
00:00:29,987 --> 00:00:34,907
The first position is zero, so index one
selects the second element.

9
00:00:34,947 --> 00:00:38,734
A stored reference connects that position
to a Python object.

10
00:00:38,760 --> 00:00:43,707
The references occupy an array in CPython,
while the objects can live elsewhere.

11
00:00:43,747 --> 00:00:46,987
The Python object is therefore separate
from its position.

12
00:00:47,027 --> 00:00:51,427
Reading or replacing an indexed reference
takes constant time under the ordinary

13
00:00:51,507 --> 00:00:52,627
interview model.

14
00:00:52,707 --> 00:00:55,347
Searching for a value can require a scan.

15
00:00:55,567 --> 00:00:58,767
Start with the list seven, two, five.

16
00:00:58,967 --> 00:01:01,847
Append nine and the new reference appears
at the end.

17
00:01:02,047 --> 00:01:06,447
Insert four at position one, and the later
references move right to make

18
00:01:06,607 --> 00:01:07,167
room.

19
00:01:07,367 --> 00:01:12,367
Remove position zero, and the first value,
seven, is returned while the remaining

20
00:01:12,647 --> 00:01:13,967
references move left.

21
00:01:14,087 --> 00:01:17,807
These movements explain the cost
difference between an end operation and a

22
00:01:17,887 --> 00:01:19,167
front operation.

23
00:01:19,207 --> 00:01:23,567
The index labels belong to positions, so
they change when values move.

24
00:01:23,800 --> 00:01:28,360
An indexed read or replacement takes
constant time because it selects one

25
00:01:28,440 --> 00:01:30,013
stored reference.

26
00:01:30,227 --> 00:01:32,920
Appending takes constant amortized time.

27
00:01:33,000 --> 00:01:37,320
Most appends use spare capacity, but a
resize can copy a linear number of

28
00:01:37,920 --> 00:01:38,600
references.

29
00:01:38,680 --> 00:01:43,000
Front insertion or removal takes linear
time because the remaining references

30
00:01:43,400 --> 00:01:43,827
move.

31
00:01:43,853 --> 00:01:46,800
A value search can also scan the whole
list.

32
00:01:46,840 --> 00:01:51,720
A slice copies the selected references, so
a slice of length k costs big

33
00:01:51,800 --> 00:01:53,800
O of k time and space.

34
00:01:54,033 --> 00:01:57,313
Assignment creates another name for the
same list.

35
00:01:57,433 --> 00:02:02,193
If alias refers to original, changing
alias at position zero changes what

36
00:02:02,273 --> 00:02:03,833
original sees.

37
00:02:03,873 --> 00:02:08,513
A slice creates a new outer list, so
replacing one of its positions does not

38
00:02:08,593 --> 00:02:10,793
replace a position in the original.

39
00:02:10,833 --> 00:02:14,246
Nested objects remain shared by a shallow
copy.

40
00:02:14,300 --> 00:02:18,673
If both outer lists refer to the same
inner list, an append to that inner list

41
00:02:18,793 --> 00:02:20,673
appears through both names.

42
00:02:20,900 --> 00:02:25,220
Read the first zero and leave write at
zero because nothing belongs in the

43
00:02:25,340 --> 00:02:27,247
retained prefix yet.

44
00:02:27,353 --> 00:02:32,220
Read seven and copy it into position zero,
then advance write to one.

45
00:02:32,260 --> 00:02:35,593
Read the next zero and leave write
unchanged.

46
00:02:35,647 --> 00:02:40,340
At every stage, the positions before write
contain exactly the nonzero values

47
00:02:40,420 --> 00:02:43,260
already encountered, in their original
order.

48
00:02:43,300 --> 00:02:48,260
Before an assignment, write never exceeds
read, so the assignment cannot erase an

49
00:02:48,340 --> 00:02:49,540
unread value.

50
00:02:49,767 --> 00:02:52,647
Read two and write it into position one.

51
00:02:52,687 --> 00:02:56,087
The retained prefix now contains seven and
two.

52
00:02:56,247 --> 00:02:58,994
Read five and write it into position two.

53
00:02:59,020 --> 00:03:01,087
The write position becomes three.

54
00:03:01,127 --> 00:03:05,727
The answer uses only the first three
positions, which contain seven, two,

55
00:03:05,767 --> 00:03:06,540
five.

56
00:03:06,594 --> 00:03:10,967
The full buffer still contains two and
five in its unused suffix.

57
00:03:11,047 --> 00:03:14,847
Those extra values are not part of the
answer because this problem asks for a

58
00:03:14,887 --> 00:03:17,127
retained prefix and its length.

59
00:03:17,367 --> 00:03:19,607
Initialize write to zero.

60
00:03:19,647 --> 00:03:23,207
The loop visits every read position in the
original list.

61
00:03:23,247 --> 00:03:25,567
A zero causes no assignment.

62
00:03:25,607 --> 00:03:30,167
For a nonzero value, the assignment copies
one reference into the next retained

63
00:03:30,607 --> 00:03:31,087
position.

64
00:03:31,127 --> 00:03:34,674
Advance write only after a retained value
is stored.

65
00:03:34,700 --> 00:03:38,194
At the end, return write instead of the
entire list.

66
00:03:38,220 --> 00:03:41,767
The condition and the update preserve the
same invariant that we used in the

67
00:03:41,847 --> 00:03:42,727
diagram.

68
00:03:42,967 --> 00:03:47,687
The function visits each of n positions
once, so its time is big O of

69
00:03:47,767 --> 00:03:48,327
n.

70
00:03:48,407 --> 00:03:53,287
Its additional storage consists of two
indices, so auxiliary space is big O

71
00:03:53,367 --> 00:03:54,220
of one.

72
00:03:54,274 --> 00:03:58,140
A printed prefix uses a slice, which
creates another list.

73
00:03:58,194 --> 00:04:00,927
That display copy is separate from the
function.

74
00:04:00,967 --> 00:04:05,607
Printing every growing prefix can require
quadratic total work, even though the

75
00:04:05,727 --> 00:04:08,247
underlying compaction function is linear.

76
00:04:08,327 --> 00:04:12,487
Explain the cost of the implementation you
will submit, including any copy it

77
00:04:12,567 --> 00:04:14,087
actually returns.

78
00:04:14,333 --> 00:04:18,013
An empty list returns zero without
entering the loop.

79
00:04:18,093 --> 00:04:21,013
An all-zero list also returns zero.

80
00:04:21,053 --> 00:04:24,440
A list with no zeros retains every
position.

81
00:04:24,466 --> 00:04:28,533
The common mistake is to return the
unchanged-length buffer as if it were a

82
00:04:28,573 --> 00:04:29,813
filtered list.

83
00:04:29,853 --> 00:04:34,333
Another problem may require moving zeros
to the end, which adds a different suffix

84
00:04:34,413 --> 00:04:35,573
requirement.

85
00:04:35,613 --> 00:04:40,493
State the result contract, trace a mixed
example, and explain why each write

86
00:04:40,573 --> 00:04:44,013
targets the current read position or an
earlier position.
